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