﻿{"id":504,"date":"2018-01-19T17:53:00","date_gmt":"2018-01-19T09:53:00","guid":{"rendered":"https:\/\/www.chenweikang.top\/?p=504"},"modified":"2018-01-19T17:53:00","modified_gmt":"2018-01-19T09:53:00","slug":"%e4%bd%bf%e7%94%a8ftpclient%e6%89%b9%e9%87%8f%e4%b8%8b%e8%bd%bd%e3%80%81%e5%88%a0%e9%99%a4ftp%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.chenweikang.top\/?p=504","title":{"rendered":"\u4f7f\u7528FTPClient\u6279\u91cf\u4e0b\u8f7d\u3001\u5220\u9664Ftp\u6587\u4ef6"},"content":{"rendered":"<blockquote><p>&lt;!---Maven\u4f9d\u8d56-&gt;<\/p>\n<p>&lt;dependency&gt;<\/p>\n<p>&lt;groupId&gt;org.apache.camel&lt;\/groupId&gt;<\/p>\n<p>&lt;artifactId&gt;camel-ftp&lt;\/artifactId&gt;<\/p>\n<p>&lt;version&gt;2.13.2&lt;\/version&gt;<\/p>\n<p>&lt;\/dependency&gt;<\/p><\/blockquote>\n<pre class=\"prettyprint linenums\">package com.ftp;\r\n\r\nimport java.io.File;\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\nimport org.apache.commons.io.IOUtils;\r\nimport org.apache.commons.net.ftp.FTP;\r\nimport org.apache.commons.net.ftp.FTPClient;\r\nimport org.apache.commons.net.ftp.FTPFile;\r\nimport org.apache.commons.net.ftp.FTPReply;\r\n\r\npublic class FtpUtil {\r\n\r\n\t\/\/ ftp\u914d\u7f6e\u76f8\u5173\u4fe1\u606f\uff0c\u53ef\u5c01\u88c5\u6210\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u505a\u6210\u914d\u7f6e\u6587\u4ef6\r\n\tprivate static FTPClient ftpClient = null;\r\n\tprivate static String hostname = \"92kk.top\";\r\n\tprivate static int port = 21;\r\n\tprivate static String username = \"chenwk\";\r\n\tprivate static String password = \"111111\";\r\n\tprivate static boolean isLogin = false;\r\n\r\n\tpublic FtpUtil() {\r\n\t\tftpClient = new FTPClient();\r\n\t\t\/\/ \u8fde\u63a5FTP\u670d\u52a1\u5668\r\n\t\ttry {\r\n\t\t\tftpClient.connect(hostname, port);\r\n\t\t\t\/\/\u8bbe\u7f6e\u6587\u4ef6\u4f20\u8f93\u7c7b\u578b,common-net\u7248\u672c\u4f4e\u4e8e2.2 \u53ea\u80fd\u5728\u767b\u5f55\u4e4b\u524d\u8bbe\u7f6efileType\uff0c\u5426\u5219\u62a5\u8fde\u63a5\u8d85\u65f6\r\n\t\t\tftpClient.enterLocalPassiveMode();\r\n\t\t\tftpClient.setFileType(FTP.BINARY_FILE_TYPE); \r\n\t\t\t\/\/ \u767b\u5f55FTP\u670d\u52a1\u5668\r\n\t\t\tftpClient.login(username, password);\r\n\t\t\t\/\/ \u9a8c\u8bc1FTP\u670d\u52a1\u5668\u662f\u5426\u767b\u5f55\u6210\u529f\r\n\t\t\tint replyCode = ftpClient.getReplyCode();\r\n\t\t\tif (!FTPReply.isPositiveCompletion(replyCode)) {\r\n\t\t\t\tSystem.out.println(\"\u767b\u5f55\u5931\u8d25\uff01\");\r\n\t\t\t} else {\r\n\t\t\t\tSystem.out.println(\"\u767b\u5f55\u6210\u529f\uff01\");\r\n\t\t\t\tisLogin = true;\r\n\t\t\t}\r\n\t\t} catch (IOException e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static FtpUtil newInstance() {\r\n\t\treturn new FtpUtil();\r\n\t}\r\n\r\n\t\r\n\r\n\t\/**\r\n\t * \u4e0b\u8f7dftp\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u5230\u6307\u5b9a\u76ee\u5f55\r\n\t * \r\n\t * @param localpath\r\n\t * @param ftpDir\r\n\t * @param isCreateRootDir \u662f\u5426\u521b\u5efa\u6307\u5b9a\u7684\u6839\u76ee\u5f55\r\n\t *\/\r\n\tpublic void downloadDirFiles(String localpath, String ftpDir , boolean isCreateRootDir , List excelList) {\r\n \t\tif (isLogin) {\r\n\t\t\tif(!isCreateRootDir) {\r\n\t\t\t\tisCreateRootDir = true;\r\n\t\t\t}else{\r\n\t\t\t\tlocalpath += \"\/\"+ftpDir;\r\n\t\t\t\tlocalpath=localpath.replace(\"\/\/\", \"\/\");\r\n\t\t\t}\r\n\t\t\tFile dir = new File(localpath);\r\n\t\t\tif (!dir.exists()) {\r\n\t\t\t\tdir.mkdirs();\r\n\t\t\t}\r\n\t\t\ttry {\r\n\t\t\t\tftpClient.changeWorkingDirectory(ftpDir);\r\n\t\t\t\tFTPFile[] ftpFiles = ftpClient.listFiles();\r\n\t\t\t\tfor (FTPFile file : ftpFiles) {\r\n\t\t\t\t\t\/\/\u9632\u6b62\u4e2d\u6587\u4e71\u7801\r\n\t\t\t\t\tString fileName = new String(file.getName().getBytes(\"iso8859-1\"), \"UTF-8\");\r\n\t\t\t\t\t\r\n\t\t\t\t\tif (file.isFile()) {\r\n\t\t\t\t\t\tFile targetFile = new File(localpath + \"\/\" + fileName);\r\n\t\t\t\t\t\tString targetFilePath =  targetFile.getAbsolutePath();\r\n\t\t\t\t\t\tSystem.out.println(\"\u4e0b\u8f7d\u6587\u4ef6\uff1a\" + targetFilePath);\r\n\t\t\t\t\t\tFileOutputStream os = new FileOutputStream(targetFile);\r\n\t\t\t\t\t\tftpClient.retrieveFile(fileName, os);\r\n\t\t\t\t\t\tIOUtils.closeQuietly(os);\r\n\t\t\t\t\t\tif(targetFilePath.contains(\".xls\") || targetFilePath.contains(\".xlsx\")){\r\n\t\t\t\t\t\t\texcelList.add(targetFilePath);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t} \r\n\t\t\t\t\tif(file.isDirectory()){\r\n\t\t\t\t\t\t\/\/ \u76ee\u5f55 ,\u9012\u5f52\u8c03\u7528\r\n\t\t\t\t\t\tdownloadDirFiles(localpath, fileName + \"\/\" , isCreateRootDir,excelList);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\t\/\/\u5f53\u524d\u76ee\u5f55\u4e0b\u8f7d\u5b8c\u6210\uff0c\u8fd4\u56de\u4e0a\u7ea7\u76ee\u5f55\r\n\t\t\t\tif (!\"\".equals(ftpDir))\r\n\t\t\t\t\tftpClient.changeWorkingDirectory(\"..\");\r\n\t\t\t} catch (Exception e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tSystem.out.println(\"\u767b\u5f55ftp\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u767b\u5f55\u914d\u7f6e\uff01\");\r\n\t\t}\r\n\t}\r\n\r\n\t\r\n\t\/**\r\n\t * \u9012\u5f52\u5220\u9664\u6307\u5b9a\u76ee\u5f55\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5305\u62ec\u6587\u4ef6\u5939\r\n\t * @param ftpDir\r\n\t * @return int  \u6210\u529f\u5220\u9664\u7684\u8bb0\u5f55\r\n\t *\/\r\n\tpublic int deleteDirFiles(String ftpDir){\r\n\t\tint count = 0;\r\n\t\ttry {\r\n\t\t\tftpClient.changeWorkingDirectory(ftpDir);\r\n\t\t\tFTPFile[] ftpFiles = ftpClient.listFiles();\r\n\t\t\tfor (FTPFile file : ftpFiles) {\r\n\t\t\t\t\/\/\u9632\u6b62\u4e2d\u6587\u4e71\u7801\r\n\t\t\t\tString fileName = new String(file.getName().getBytes(\"iso8859-1\"), \"UTF-8\");\r\n\t\t\t\tif (file.isFile()) {\r\n\t\t\t\t\tboolean f = ftpClient.deleteFile(fileName);\r\n\t\t\t\t\tif(f){\r\n\t\t\t\t\t\tSystem.out.println(\"\u5220\u9664\u6587\u4ef6-\u6210\u529f\uff1a\" + fileName);\r\n\t\t\t\t\t}else{\r\n\t\t\t\t\t\tSystem.out.println(\"\u5220\u9664\u6587\u4ef6-\u5931\u8d25\uff1a\" + fileName);\r\n\t\t\t\t\t\tcount++;\r\n\t\t\t\t\t}\r\n\t\t\t\t} else if(file.isDirectory()){\r\n\t\t\t\t\t\/\/\u9012\u5f52\u8c03\u7528\r\n\t\t\t\t\tdeleteDirFiles(fileName + \"\/\");\r\n\t\t\t\t\tboolean f =  ftpClient.removeDirectory(fileName);\r\n\t\t\t\t\tif(f){\r\n\t\t\t\t\t\tSystem.out.println(\"\u5220\u9664\u6587\u4ef6\u5939-\u6210\u529f\uff1a\" + fileName);\r\n\t\t\t\t\t\tcount++;\r\n\t\t\t\t\t}else{\r\n\t\t\t\t\t\tSystem.out.println(\"\u5220\u9664\u6587\u4ef6\u5939-\u5931\u8d25\uff1a\" + fileName);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\/\/\u5f53\u524d\u76ee\u5f55\u5220\u9664\u5b8c\u6210\uff0c\u8fd4\u56de\u4e0a\u7ea7\u76ee\u5f55\r\n\t\t\tif (!\"\".equals(ftpDir)){\r\n\t\t\t\tftpClient.changeWorkingDirectory(\"..\");\r\n\t\t\t}\r\n\t\t\t\t\r\n\t\t} catch (IOException e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t\treturn count;\r\n\t}\r\n\t\r\n\t\r\n\t\/**\r\n\t * \u8bfb\u53d6\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u6587\u4ef6\u540d\r\n\t *\r\n\t * @param pathname\r\n\t * @return\r\n\t * @throws IOException\r\n\t *\/\r\n\r\n\tpublic List getFileList(String pathname) {\r\n\t\tList fileLists = new ArrayList();\r\n\t\tif (isLogin) {\r\n\t\t\t\/\/ \u83b7\u5f97\u6307\u5b9a\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u540d\r\n\t\t\tFTPFile[] ftpFiles = null;\r\n\t\t\ttry {\r\n\t\t\t\tftpFiles = ftpClient.listFiles(pathname);\r\n\t\t\t\tfor (int i = 0; ftpFiles != null &amp;&amp; i &lt; ftpFiles.length; i++) {\r\n\t\t\t\t\tFTPFile file = ftpFiles[i];\r\n\t\t\t\t\tif (file.isFile()) {\r\n\t\t\t\t\t\tfileLists.add(new String(file.getName().getBytes(\"iso8859-1\"), \"UTF-8\"));\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t} catch (IOException e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tSystem.out.println(\"\u767b\u5f55ftp\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u767b\u5f55\u914d\u7f6e\uff01\");\r\n\t\t}\r\n\t\treturn fileLists;\r\n\t}\r\n\r\n\t\r\n\t\/**\r\n\t * \u9000\u51fa\u767b\u5f55\r\n\t *\/\r\n\tpublic void logout() {\r\n\t\tif (ftpClient.isConnected()) {\r\n\t\t\ttry {\r\n\t\t\t\tif (ftpClient != null)\r\n\t\t\t\t\tftpClient.logout();\r\n\t\t\t\tSystem.out.println(\"\u9000\u51fa\u767b\u5f55\uff01\");\r\n\t\t\t} catch (IOException e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n<hr \/>\n<h3>Main\u65b9\u6cd5\u6d4b\u8bd5\u7c7b<\/h3>\n<pre class=\"prettyprint linenums\">public static void main(String[] args) {\r\n\t\t\/\/\u6307\u5b9aftp\u76ee\u5f55\r\n\t\tString pathname = \"\/product\/\";\r\n\t        FtpUtil ftpUtil = FtpUtil.newInstance();\r\n\t        List excelList = new ArrayList&lt;&gt;();\r\n \t        ftpUtil.downloadDirFiles(\"E:\\\\downFtpPath\",pathname,true,excelList);\r\n\t        ftpUtil.deleteDirFiles(pathname);\r\n\t        System.out.println( excelList.toString());\r\n\t        ftpUtil.logout();\r\n\t}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.chenweikang.top\">\u5de6\u624b\u4ee3\u7801\u53f3\u624b\u8bd7<\/a> &raquo; <a href=\"https:\/\/www.chenweikang.top\/?p=504\">\u4f7f\u7528FTPClient\u6279\u91cf\u4e0b\u8f7d\u3001\u5220\u9664Ftp\u6587\u4ef6<\/a><\/p><div class=\"__youshang\">\r\n            <div id=\"__youshang_popup\" class=\"wechat popup\" style=\"display: none;\">\r\n                <div class=\"head\">~\u8c22\u8c22\u6253\u8d4f~<\/div>\r\n                <div class=\"qrcode\"><div class=\"qrcode-li wechat\" ><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/wexin.png\" \/><\/div><div class=\"qrcode-li alipay\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-pay.png\" \/><\/div><div class=\"qrcode-li hongbao\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-hongbao.png\" \/><\/div><\/div>\r\n                <ul class=\"platform\"><li class=\"icon-wechat active\" data-bg-color=\"#05af4e\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-alipay \" data-bg-color=\"#00a2ea\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-hongbao \" data-bg-color=\"#dd5746\" data-thanks=\"<p>\u626b\u7801\u9886\u7ea2\u5305<\/p><p style='margin-top: 24px;'>\uff08\u4f59\u989d\u5b9d\u652f\u4ed8\u65f6\u53ef\u62b5\u73b0\uff09<\/p>\"><\/li><\/ul>\r\n            <\/div>\r\n            <a href=\"javascript:void(0);\" id=\"__youshang_btn\">\u8d4f<\/a>\r\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>&lt;!---Maven\u4f9d\u8d56-&gt; &lt;dependency&gt; &lt;groupId&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[53,20],"class_list":["post-504","post","type-post","status-publish","format-standard","hentry","category-java","tag-ftpclient","tag-java"],"_links":{"self":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=504"}],"version-history":[{"count":0,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/504\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}