In Java programs, often need to deal with FTP, such as uploading files to the FTP server, download files, this article briefly describes how to use the Jakarta Commons in the FtpClient (in the commons-net package) implementation of the upload download file.
First, upload files
The principle is not introduced, we directly look at the code bar
1/** 2 * Description: Uploading files to the FTP server 3 * @Version1.0 Jul, 4:31:09 PM by Tri Hongbao ([email protected]) Create 4 * @param URL FTP server hostname 5 * @param port FTP Server ports 6 * @param username FTP login Account 7 * @param passwor d FTP Login Password 8 * @param path FTP Server save directory 9 * @param filename uploaded to FTP server file name * @param input stream one * @retu RN successfully returns TRUE, otherwise false12 */13 public static Boolean UploadFile (String url,int port,string username, string passwor D, string path, string filename, InputStream input) {success = false;15 ftpclient FTP = new FTP Client (), try {19 int reply;18 ftp.connect (URL, port),///Connect FTP server (+//), if default port is used. Port, you can use Ftp.connect (URL) to connect directly to the FTP server Ftp.login (username, password);//Login reply = Ftp.getreplycod E (); Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect (); return success;25 }26 ftp.changeworkingdirectory (path); ftp.storefile (filename, input); Input.close (); Ftp.logout (); success = true;32} catch ( IOException e) {e.printstacktrace (); {36} finally {+ if (ftp.isconnected ())} try {PNS ftp.disconnect (); IOException IoE) {39}40 }41}42 return success;43}<pre></pre>
Below we write two small examples:
1. Upload the local file to the FTP server with the following code:
1 @Test 2 public void Testuploadfromdisk () {3 try {4 fileinputstream in=new fileinputstream (New File (" D:/test.txt ")); 5 Boolean flag = UploadFile ("127.0.0.1", +, "test", "Test", "D:/ftp", "test.txt", in); 6 System.out.println ( Flag); 7 } catch (FileNotFoundException e) {8 e.printstacktrace (); 9 }10 }<pre></pre>
2. Generate a file on the FTP server and write a string to the file
1 @Test 2 public void Testuploadfromstring () {3 try {4 inputstream input = new Bytearrayinputstream (" Test FTP ". GetBytes (" Utf-8 ")); 5 Boolean flag = UploadFile ("127.0.0.1", +, "test", "Test", "D:/ftp", "test.txt", input), 6 System.out.println ( Flag); 7 } catch (Unsupportedencodingexception e) {8 e.printstacktrace (); 9 }10 }<pre></pre>
Second, download the file
The code to download the file from the FTP server is also very simple, as follows:
1/** 2 * Description: Download files from FTP server 3 * @Version1.0 Jul, 5:32:36 PM by Tri Hongbao ([email protected]) Create 4 * @param URL FTP server hostname 5 * @param port FTP Server ports 6 * @param username FTP login Account 7 * @param passwor d FTP Login Password 8 * @param relative path on the RemotePath FTP server 9 * @param filename to download file name * @param localPath Download to local path 11 * @return12 */13 public static Boolean downfile (string url, int port,string username, string password, stri ng remotepath,string filename,string LocalPath) {+ Boolean success = false;15 ftpclient ftp = new Ftpclie NT (); reply;18 ftp.connect (URL, port), and 19//If the default port is used, you can use the Ftp.conn ECT (URL) direct connection to FTP server Ftp.login (username, password);//Login reply = Ftp.getreplycode (); 22 if (! Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect (); return success;25 }26 Ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directory ftpfile[] fs = Ftp.listfiles (); Tpfile ff:fs) {if (Ff.getname (). Equals (FileName)) {File LocalFile = new file (Localp ath+ "/" +ff.getname ()); OutputStream is = new FileOutputStream (localfile); Ftp.retrievefile (Ff.getname (), is); Is.close (); 35}36 }37 Ftp.logout (); success = true;40} catch (IOException e) {41 E.printstacktrace (); finally {ftp.isconnected ()) {45 Ftp.disconnect (); IOException IoE) {47}48}49 }50 return success;51}
Upload downloads in Java using ftpclient