{"id":307,"date":"2025-08-29T04:37:06","date_gmt":"2025-08-29T04:37:06","guid":{"rendered":"https:\/\/iotnoob.com\/wordpress\/?p=307"},"modified":"2025-09-09T10:09:42","modified_gmt":"2025-09-09T10:09:42","slug":"ftp-upload-file-%e0%b8%94%e0%b9%89%e0%b8%a7%e0%b8%a2-java","status":"publish","type":"post","link":"https:\/\/iotnoob.com\/wordpress\/2025\/08\/29\/ftp-upload-file-%e0%b8%94%e0%b9%89%e0%b8%a7%e0%b8%a2-java\/","title":{"rendered":"FTP upload file \u0e14\u0e49\u0e27\u0e22 Java"},"content":{"rendered":"\n<p>\u0e15\u0e34\u0e14\u0e15\u0e31\u0e49\u0e07 FTP server \u0e01\u0e48\u0e2d\u0e19 &#8211; \u0e43\u0e0a\u0e49 Filezilla Server<\/p>\n\n\n\n<p>\u0e40\u0e1b\u0e34\u0e14\u0e43\u0e0a\u0e49\u0e07\u0e32\u0e19 services -> Filezilla-server<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"488\" src=\"https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-98-1024x488.png\" alt=\"\" class=\"wp-image-308\" srcset=\"https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-98-1024x488.png 1024w, https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-98-300x143.png 300w, https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-98-768x366.png 768w, https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-98.png 1118w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u0e40\u0e25\u0e37\u0e2d\u0e01\u0e43\u0e0a\u0e49\u0e07\u0e32\u0e19 Apache commons net<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"802\" height=\"407\" src=\"https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-99.png\" alt=\"\" class=\"wp-image-309\" srcset=\"https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-99.png 802w, https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-99-300x152.png 300w, https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-99-768x390.png 768w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n * Click nbfs:\/\/nbhost\/SystemFileSystem\/Templates\/Licenses\/license-default.txt to change this license\n * Click nbfs:\/\/nbhost\/SystemFileSystem\/Templates\/Classes\/Main.java to edit this template\n *\/\npackage ftpuploader;\n\n\/**\n *\n * @author Thada\n *\/\nimport org.apache.commons.net.ftp.FTP;\nimport org.apache.commons.net.ftp.FTPClient;\n\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.PrintStream;\n\n\/**\n * \u0e04\u0e25\u0e32\u0e2a\u0e19\u0e35\u0e49\u0e2a\u0e32\u0e18\u0e34\u0e15\u0e27\u0e34\u0e18\u0e35\u0e01\u0e32\u0e23\u0e2d\u0e31\u0e1b\u0e42\u0e2b\u0e25\u0e14\u0e44\u0e1f\u0e25\u0e4c\u0e44\u0e1b\u0e22\u0e31\u0e07\u0e40\u0e0b\u0e34\u0e23\u0e4c\u0e1f\u0e40\u0e27\u0e2d\u0e23\u0e4c FTP \u0e42\u0e14\u0e22\u0e43\u0e0a\u0e49\u0e44\u0e25\u0e1a\u0e23\u0e32\u0e23\u0e35 Apache Commons Net\n * \u0e04\u0e38\u0e13\u0e08\u0e33\u0e40\u0e1b\u0e47\u0e19\u0e15\u0e49\u0e2d\u0e07\u0e40\u0e1e\u0e34\u0e48\u0e21 dependency \u0e02\u0e2d\u0e07 Apache Commons Net \u0e43\u0e19\u0e42\u0e1b\u0e23\u0e40\u0e08\u0e01\u0e15\u0e4c\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\u0e01\u0e48\u0e2d\u0e19\n *\n * \u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a Maven, \u0e40\u0e1e\u0e34\u0e48\u0e21\u0e43\u0e19\u0e44\u0e1f\u0e25\u0e4c pom.xml:\n * &lt;dependency>\n * &lt;groupId>commons-net&lt;\/groupId>\n * &lt;artifactId>commons-net&lt;\/artifactId>\n * &lt;version>3.11.0&lt;\/version>\n * &lt;\/dependency>\n *\/\npublic class FTPUploader {\n\n    private final String server;\n    private final int port;\n    private final String user;\n    private final String pass;\n\n    \/**\n     * Constructor \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e23\u0e31\u0e1a\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e01\u0e32\u0e23\u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d FTP\n     * @param server \u0e17\u0e35\u0e48\u0e2d\u0e22\u0e39\u0e48\u0e02\u0e2d\u0e07 FTP server (\u0e40\u0e0a\u0e48\u0e19 ftp.example.com)\n     * @param port \u0e1e\u0e2d\u0e23\u0e4c\u0e15\u0e02\u0e2d\u0e07 FTP server (\u0e1b\u0e01\u0e15\u0e34\u0e04\u0e37\u0e2d 21)\n     * @param user \u0e0a\u0e37\u0e48\u0e2d\u0e1c\u0e39\u0e49\u0e43\u0e0a\u0e49\n     * @param pass \u0e23\u0e2b\u0e31\u0e2a\u0e1c\u0e48\u0e32\u0e19\n     *\/\n    public FTPUploader(String server, int port, String user, String pass) {\n        this.server = server;\n        this.port = port;\n        this.user = user;\n        this.pass = pass;\n    }\n\n    \/**\n     * \u0e40\u0e21\u0e18\u0e2d\u0e14\u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a\u0e2d\u0e31\u0e1b\u0e42\u0e2b\u0e25\u0e14\u0e44\u0e1f\u0e25\u0e4c\u0e40\u0e14\u0e35\u0e22\u0e27\u0e44\u0e1b\u0e22\u0e31\u0e07\u0e40\u0e0b\u0e34\u0e23\u0e4c\u0e1f\u0e40\u0e27\u0e2d\u0e23\u0e4c FTP\n     * @param localFilePath \u0e1e\u0e32\u0e18\u0e02\u0e2d\u0e07\u0e44\u0e1f\u0e25\u0e4c\u0e1a\u0e19\u0e40\u0e04\u0e23\u0e37\u0e48\u0e2d\u0e07\u0e04\u0e2d\u0e21\u0e1e\u0e34\u0e27\u0e40\u0e15\u0e2d\u0e23\u0e4c\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\n     * @param remoteFilePath \u0e1e\u0e32\u0e18\u0e41\u0e25\u0e30\u0e0a\u0e37\u0e48\u0e2d\u0e44\u0e1f\u0e25\u0e4c\u0e17\u0e35\u0e48\u0e15\u0e49\u0e2d\u0e07\u0e01\u0e32\u0e23\u0e1a\u0e31\u0e19\u0e17\u0e36\u0e01\u0e1a\u0e19\u0e40\u0e0b\u0e34\u0e23\u0e4c\u0e1f\u0e40\u0e27\u0e2d\u0e23\u0e4c FTP\n     * @throws IOException \u0e40\u0e21\u0e37\u0e48\u0e2d\u0e40\u0e01\u0e34\u0e14\u0e1b\u0e31\u0e0d\u0e2b\u0e32\u0e43\u0e19\u0e01\u0e32\u0e23\u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d\u0e2b\u0e23\u0e37\u0e2d\u0e01\u0e32\u0e23\u0e2a\u0e48\u0e07\u0e44\u0e1f\u0e25\u0e4c\n     *\/\n    public void uploadFile(String localFilePath, String remoteFilePath) throws IOException {\n        FTPClient ftpClient = new FTPClient();\n        try {\n            \/\/ 1. \u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d\u0e41\u0e25\u0e30\u0e25\u0e47\u0e2d\u0e01\u0e2d\u0e34\u0e19\n            System.out.println(\"Connecting to \" + server + \"...\");\n            ftpClient.connect(server, port);\n            boolean loggedIn = ftpClient.login(user, pass);\n\n            if (!loggedIn) {\n                System.out.println(\"Cannot login, please check user &amp; password\");\n                return;\n            }\n            System.out.println(\"Login successfully\");\n\n            \/\/ 2. \u0e15\u0e31\u0e49\u0e07\u0e04\u0e48\u0e32\u0e42\u0e2b\u0e21\u0e14\u0e41\u0e25\u0e30\u0e1b\u0e23\u0e30\u0e40\u0e20\u0e17\u0e44\u0e1f\u0e25\u0e4c\n            ftpClient.enterLocalPassiveMode(); \/\/ \u0e42\u0e2b\u0e21\u0e14 Passive \u0e2a\u0e33\u0e04\u0e31\u0e0d\u0e21\u0e32\u0e01\u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a\u0e40\u0e04\u0e23\u0e37\u0e2d\u0e02\u0e48\u0e32\u0e22\u0e17\u0e35\u0e48\u0e21\u0e35 Firewall\n            ftpClient.setFileType(FTP.BINARY_FILE_TYPE); \/\/ \u0e15\u0e31\u0e49\u0e07\u0e40\u0e1b\u0e47\u0e19\u0e42\u0e2b\u0e21\u0e14\u0e44\u0e1a\u0e19\u0e32\u0e23\u0e35\u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e23\u0e2d\u0e07\u0e23\u0e31\u0e1a\u0e44\u0e1f\u0e25\u0e4c\u0e17\u0e38\u0e01\u0e1b\u0e23\u0e30\u0e40\u0e20\u0e17\n\n            \/\/ 3. \u0e40\u0e15\u0e23\u0e35\u0e22\u0e21\u0e44\u0e1f\u0e25\u0e4c\u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a\u0e2d\u0e31\u0e1b\u0e42\u0e2b\u0e25\u0e14\n            File localFile = new File(localFilePath);\n            if (!localFile.exists()) {\n                System.out.println(\"Cannot find: \" + localFilePath);\n                return;\n            }\n\n            \/\/ 4. \u0e17\u0e33\u0e01\u0e32\u0e23\u0e2d\u0e31\u0e1b\u0e42\u0e2b\u0e25\u0e14\n            try (InputStream inputStream = new FileInputStream(localFile)) {\n                System.out.println(\"Start uploading the file: \" + localFile.getName());\n                boolean done = ftpClient.storeFile(remoteFilePath, inputStream);\n                if (done) {\n                    System.out.println(\"File '\" + localFile.getName() + \"' uploaded successfully\");\n                } else {\n                    System.out.println(\"Upload fail\");\n                    \/\/ \u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e15\u0e23\u0e27\u0e08\u0e2a\u0e2d\u0e1a reply code \u0e40\u0e1e\u0e34\u0e48\u0e21\u0e40\u0e15\u0e34\u0e21\u0e44\u0e14\u0e49\n                    \/\/ System.out.println(\"FTP Reply: \" + ftpClient.getReplyString());\n                }\n            }\n\n        } finally {\n            \/\/ 5. \u0e2d\u0e2d\u0e01\u0e08\u0e32\u0e01\u0e23\u0e30\u0e1a\u0e1a\u0e41\u0e25\u0e30\u0e15\u0e31\u0e14\u0e01\u0e32\u0e23\u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d\u0e40\u0e2a\u0e21\u0e2d\n            try {\n                if (ftpClient.isConnected()) {\n                    ftpClient.logout();\n                    ftpClient.disconnect();\n                    System.setOut(new PrintStream(System.out, true, \"UTF-8\"));\n                    System.out.println(\"System.encoding: \" + System.getProperty(\"file.encoding\"));\n                    System.out.println(\"Disconnected\");\n                    System.out.println(\"\u0e15\u0e31\u0e14\u0e01\u0e32\u0e23\u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d\u0e41\u0e25\u0e49\u0e27\");\n                }\n            } catch (IOException ex) {\n                ex.printStackTrace();\n            }\n        }\n    }\n\n    \/**\n     * \u0e40\u0e21\u0e18\u0e2d\u0e14 main \u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a\u0e17\u0e14\u0e2a\u0e2d\u0e1a\u0e01\u0e32\u0e23\u0e17\u0e33\u0e07\u0e32\u0e19\n     *\/\n    public static void main(String[] args) {\n        \/\/ --- \u0e01\u0e23\u0e38\u0e13\u0e32\u0e01\u0e23\u0e2d\u0e01\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25 FTP \u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\u0e17\u0e35\u0e48\u0e19\u0e35\u0e48 ---\n        String server = \"127.0.0.1\"; \/\/ &lt;--- \u0e43\u0e2a\u0e48\u0e0a\u0e37\u0e48\u0e2d\u0e40\u0e0b\u0e34\u0e23\u0e4c\u0e1f\u0e40\u0e27\u0e2d\u0e23\u0e4c\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\n        int port = 21;\n        String user = \"th\";     \/\/ &lt;--- \u0e43\u0e2a\u0e48\u0e0a\u0e37\u0e48\u0e2d\u0e1c\u0e39\u0e49\u0e43\u0e0a\u0e49\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\n        String pass = \"12345678\";      \/\/ &lt;--- \u0e43\u0e2a\u0e48\u0e23\u0e2b\u0e31\u0e2a\u0e1c\u0e48\u0e32\u0e19\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\n        \/\/ ------------------------------------\n\n        FTPUploader uploader = new FTPUploader(server, port, user, pass);\n\n        try {\n            \/\/ --- \u0e23\u0e30\u0e1a\u0e38\u0e1e\u0e32\u0e18\u0e44\u0e1f\u0e25\u0e4c\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13 ---\n            \/\/ \u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a Windows: \"C:\/Users\/YourUser\/Documents\/report.pdf\"\n            \/\/ \u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a macOS\/Linux: \"\/home\/youruser\/documents\/report.pdf\"\n            File localFilePath = new File(\"C:\\\\Users\\\\Thada\\\\Desktop\\\\gen.info\\\\attribute.txt\"); \/\/ &lt;--- \u0e43\u0e2a\u0e48\u0e1e\u0e32\u0e18\u0e44\u0e1f\u0e25\u0e4c\u0e43\u0e19\u0e40\u0e04\u0e23\u0e37\u0e48\u0e2d\u0e07\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\n            String localFileName = localFilePath.getName();\n            String remoteFilePath = \"\/tmp\/\" + localFileName; \/\/ &lt;--- \u0e43\u0e2a\u0e48\u0e1e\u0e32\u0e18\u0e1a\u0e19\u0e40\u0e0b\u0e34\u0e23\u0e4c\u0e1f\u0e40\u0e27\u0e2d\u0e23\u0e4c\n            \/\/ -------------------------\n\n            uploader.uploadFile(localFilePath.toString(), remoteFilePath);\n\n        } catch (IOException ex) {\n            System.err.println(\"\u0e40\u0e01\u0e34\u0e14\u0e02\u0e49\u0e2d\u0e1c\u0e34\u0e14\u0e1e\u0e25\u0e32\u0e14\u0e23\u0e49\u0e32\u0e22\u0e41\u0e23\u0e07\u0e23\u0e30\u0e2b\u0e27\u0e48\u0e32\u0e07\u0e01\u0e32\u0e23\u0e2d\u0e31\u0e1b\u0e42\u0e2b\u0e25\u0e14\");\n            ex.printStackTrace();\n        }\n    }\n}\n\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Connecting to 127.0.0.1...\nLogin successfully\nStart uploading the file: attribute.txt\nFile 'attribute.txt' uploaded successfully\nSystem.encoding: UTF-8\nDisconnected\n\u0e15\u0e31\u0e14\u0e01\u0e32\u0e23\u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d\u0e41\u0e25\u0e49\u0e27<\/pre>\n\n\n\n<p>\u0e16\u0e49\u0e32\u0e44\u0e21\u0e48\u0e44\u0e14\u0e49\u0e40\u0e1b\u0e34\u0e14 service \u0e40\u0e2d\u0e32\u0e44\u0e27\u0e49<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Connecting to 127.0.0.1...\n\u0e40\u0e01\u0e34\u0e14\u0e02\u0e49\u0e2d\u0e1c\u0e34\u0e14\u0e1e\u0e25\u0e32\u0e14\u0e23\u0e49\u0e32\u0e22\u0e41\u0e23\u0e07\u0e23\u0e30\u0e2b\u0e27\u0e48\u0e32\u0e07\u0e01\u0e32\u0e23\u0e2d\u0e31\u0e1b\u0e42\u0e2b\u0e25\u0e14\njava.net.ConnectException: Connection refused: getsockopt\n\tat java.base\/sun.nio.ch.Net.pollConnect(Native Method)\n\tat java.base\/sun.nio.ch.Net.pollConnectNow(Net.java:628)\n\tat java.base\/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:533)\n\tat java.base\/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:583)\n\tat java.base\/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:282)\n\tat java.base\/java.net.Socket.connect(Socket.java:665)\n\tat org.apache.commons.net.SocketClient._connect(SocketClient.java:141)\n\tat org.apache.commons.net.SocketClient.connect(SocketClient.java:308)\n\tat org.apache.commons.net.SocketClient.connect(SocketClient.java:290)\n\tat ftpuploader.FTPUploader.uploadFile(FTPUploader.java:64)\n\tat ftpuploader.FTPUploader.main(FTPUploader.java:136)\nBUILD SUCCESSFUL (total time: 0 seconds)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0e15\u0e34\u0e14\u0e15\u0e31\u0e49\u0e07 FTP server \u0e01\u0e48\u0e2d\u0e19 &#8211; \u0e43\u0e0a\u0e49 Filezilla Server \u0e40\u0e1b\u0e34\u0e14\u0e43\u0e0a\u0e49\u0e07\u0e32\u0e19 services -> Filezilla-server \u0e40\u0e25\u0e37\u0e2d\u0e01\u0e43\u0e0a\u0e49\u0e07\u0e32\u0e19 Apache commons&hellip;<\/p>\n","protected":false},"author":1,"featured_media":341,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,2],"tags":[],"class_list":["post-307","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-programming"],"_links":{"self":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/comments?post=307"}],"version-history":[{"count":2,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/307\/revisions\/312"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media\/341"}],"wp:attachment":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}