{"id":2240,"date":"2023-05-07T10:10:25","date_gmt":"2023-05-07T02:10:25","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2240"},"modified":"2023-05-07T10:11:09","modified_gmt":"2023-05-07T02:11:09","slug":"groovy-code-example-download-files-with-ftp","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/07\/groovy-code-example-download-files-with-ftp\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; groovy\u4e2d\u4f7f\u7528ftp\u4e0b\u8f7d\u6587\u4ef6"},"content":{"rendered":"<p>\u524d\u9762\u8bb2\u4e86\u5728groovy\u4e2d\u4f7f\u7528http\u7684\u4e00\u4e9b\u6280\u5de7\u3002\u672c\u6587\u4e3b\u8981\u8bf4\u4e00\u4e0b\u5982\u4f55\u5728groovy\u4e2d\u4f7f\u7528ftp\u3002<\/p>\n<p>1\uff09\u9996\u5148\u5f15\u5165groovy ftp\u5305<\/p>\n<pre><code class=\"language-java\">import org.apache.commons.net.ftp.FTPClient<\/code><\/pre>\n<p>2\uff09\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u5b9e\u4f8b\u5316ftpclient\u5e76\u8fde\u63a5FTP\u4e86<\/p>\n<pre><code class=\"language-java\">new FTPClient().with { \/\/\u5b9e\u4f8b\u5316FTPClient\n  try {\n    connect &#039;192.168.1.100&#039; \/\/\u8fde\u63a5ftp\n    enterLocalPassiveMode() \/\/\u542f\u7528\u88ab\u52a8\u6a21\u5f0f\n    setFileType(BINARY_FILE_TYPE) \/\/\u8bbe\u7f6eFTP\u6587\u4ef6\u7c7b\u578b\u4e3aBIN\n    login &#039;ftpuser_1&#039;,&#039;123456&#039; \/\/\u767b\u5f55FTP\n    if (!changeWorkingDirectory(&quot;\/dat\/$date&quot;)) {\n      \/\/\u5c1d\u8bd5\u6539\u53d8\u670d\u52a1\u5668\u5f53\u524d\u76ee\u5f55\uff0c\u8fd9\u6bb5\u4ee3\u7801\u7684\u76ee\u7684\u662f\u6d4b\u8bd5\u8def\u5f84\u662f\u4e0d\u662f\u5b58\u5728\uff0c$date\u4e3a\u5f53\u5929\u65e5\u671fyyyyMMdd\n      log &#039;no files found&#039;\n      return\n    }\n    def files = listFiles() \/\/\u5217\u51fa\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\n    if (files.length==0) {\n      return;\n    }\n\n    for (def file: files) {\n      def incomingFile = new File(&quot;$path\/$date\/${file.name}&quot;) \/\/\u672c\u5730\u6587\u4ef6\n      incomingFile.withOutputStream {ostream -&gt; retrieveFile file.name, ostream } \/\/\u5f00\u59cb\u4e0b\u8f7d\n    }\n  } finally {\n    logout() \/\/FTP \u767b\u51fa\n    disconnect() \/\/\u65ad\u94fe\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u9762\u8bb2\u4e86\u5728groovy\u4e2d\u4f7f\u7528http\u7684\u4e00\u4e9b\u6280\u5de7\u3002\u672c\u6587\u4e3b\u8981\u8bf4\u4e00\u4e0b\u5982\u4f55\u5728groovy\u4e2d\u4f7f\u7528ftp\u3002 1\uff09\u9996\u5148\u5f15\u5165g [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[568],"tags":[],"class_list":["post-2240","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2240"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2240\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}