Java implementation of FTP file download source code

Source: Internet
Author: User
Tags ftp file ftp file transfer

These days to do a project in the use of FTP file transfer, Java implementation of the ftp file download.

First, win7 the FTP server to build

See: http://jingyan.baidu.com/article/4b52d7026f50eefc5c774bcc.html

Second, the implementation of Java FTP file download

Using the Org.apache.commons.net jar package,: http://commons.apache.org/proper/commons-net/download_net.cgi

After the download is complete, create a new Java project and import the Commons-net-3.3.jar package with the following source code:

Import Java.io.file;import java.io.fileoutputstream;import Java.io.ioexception;import Java.io.OutputStream;import Org.apache.commons.net.ftp.ftpclient;import Org.apache.commons.net.ftp.ftpfile;import Org.apache.commons.net.ftp.ftpreply;public class Ftpdownload {public static Boolean downfile (String url, int port,      String Username, string password, string remotepath,string filename,string LocalPath) {Boolean success = false;      ftpclient ftp = new FtpClient ();          try {int reply;          Ftp.connect (URL, port); If using the default port, you can use Ftp.connect (URL) to connect directly to the FTP server Ftp.login (username, password);//Login reply = Ftp.getreplycode          (); if (!              Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();          return success;          } ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directory ftpfile[] fs = Ftp.listfiles (); for (Ftpfile ff:fs) {if (Ff.getname (). Equals (FileName)) {File LocaLfile = new File (localpath+ "/" +ff.getname ());                   OutputStream is = new FileOutputStream (localfile);                  Ftp.retrievefile (Ff.getname (), is);              Is.close ();          }} ftp.logout ();      Success = true;      } catch (IOException e) {e.printstacktrace ();              } finally {if (ftp.isconnected ()) {try {ftp.disconnect ()};  } catch (IOException IoE) {}}} return success; }public static void Main (string[] args) {//boolean flag = downfile ("202.114.21.202", +//"ASUS", "etc2015", "F:\\ftpdown Load "," Test.txt "," F:\\java "); Boolean flag = Downfile (" 202.114.21.22 ", +," Zdt "," Zdt "," E:\\ftptest "," 20141126172643 -xlw-x14.jpg "," F:\\java "); if (flag) {System.out.println (" Download successful!) ");} else {System.out.println ("Download failed! ");}}}

Run the program and test to find the FTP file downloaded successfully!



Java implementation of FTP file download source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.