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
/*** Description: Uploading files to FTP server * @Version1.0 Jul, 4:31:09 PM by Tri Hongbao ([email protected]) Create *@paramURL ftp server hostname*@paramPort FTP Server ports *@paramUsername FTP login Account *@paramPassword FTP login password *@paramPath FTP Server save directory *@paramfilename uploaded to the FTP server *@paramInput Stream *@returnSuccess returns True, otherwise false*/publicstaticboolean uploadfile (String URL,intport,string Username, string password, string path, string filename, InputStream input) { BooleanSuccess =false; FtpClient FTP=Newftpclient (); Try { intreply; Ftp.connect (URL, port);//connecting to an FTP server//If you use the default port, you can connect directly to the FTP server using Ftp.connect (URL)Ftp.login (username, password);//LoginReply =Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply)) {Ftp.disconnect (); returnsuccess; } ftp.changeworkingdirectory (path); Ftp.storefile (filename, input); Input.close (); Ftp.logout (); Success=true; } Catch(IOException e) {e.printstacktrace (); } finally { if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException IoE) {}}} returnsuccess;}
/*** Description: Uploading files to FTP server * @Version1.0 Jul, 4:31:09 PM by Tri Hongbao ([email protected]) Create *@paramURL FTP server hostname *@paramPort FTP Server ports *@paramUsername FTP login Account *@paramPassword FTP login password *@paramPath FTP Server save directory *@paramfilename uploaded to the FTP server *@paramInput Stream *@returnSuccess returns True, otherwise false*/ Public Static BooleanUploadFile (String URL,intport,string Username, string password, string path, string filename, InputStream input) { BooleanSuccess =false; FtpClient FTP=Newftpclient (); Try { intreply; Ftp.connect (URL, port);//connecting to an FTP server//If you use the default port, you can connect directly to the FTP server using Ftp.connect (URL)Ftp.login (username, password);//LoginReply =Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply)) {Ftp.disconnect (); returnsuccess; } ftp.changeworkingdirectory (path); Ftp.storefile (filename, input); Input.close (); Ftp.logout (); Success=true; } Catch(IOException e) {e.printstacktrace (); } finally { if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException IoE) {}}} returnsuccess; }
Below we write two small examples:
1. Upload the local file to the FTP server with the following code:
@Test publicvoid Testuploadfromdisk () { {FileInputStream in =new File InputStream (new File ("D:/test.txt" boolean flag = UploadFile ("127.0.0.1", +, "test", "Test", "D:/ftp", " Test.txt ", in); SYSTEM.OUT.PRINTLN (flag); catch (FileNotFoundException e) {E.PR Intstacktrace (); } }
@Test Public void Testuploadfromdisk () { try { fileinputstreamin =new FileInputStream ( New File ("D:/test.txt")); boolean flag = UploadFile ("127.0.0.1", +, "test", "Test", "D:/ftp", "test.txt",in); SYSTEM.OUT.PRINTLN (flag); Catch (FileNotFoundException e) { e.printstacktrace (); } }
2. Generate a file on the FTP server and write a string to the file
@Test publicvoid testuploadfromstring () { {InputStream input = new Byt Earrayinputstream ("Test ftp". GetBytes ("Utf-8" )); boolean flag = UploadFile ("127.0.0.1", +, "test", "Test", "D:/ftp", " Test.txt " catch (Unsupportedencodingexception E ) {e.printstacktrace (); } }
@Test Public void testuploadfromstring () { try { new bytearrayinputstream ("Test FTP"). GetBytes ("Utf-8")); boolean flag = UploadFile ("127.0.0.1", +, "test", "Test", "D:/ftp", "test.txt", input); SYSTEM.OUT.PRINTLN (flag); Catch (unsupportedencodingexception e) { e.printstacktrace (); } }
Second, download the file
The code to download the file from the FTP server is also very simple, as follows:
/*** Description: Download files from FTP server * @Version1.0 Jul, 5:32:36 PM by Tri Hongbao ([email protected]) Create *@paramURL ftp server hostname*@paramPort FTP Server ports *@paramUsername FTP login Account *@paramPassword FTP login password *@paramRemotePath The relative path on the FTP server *@paramfilename to download *@paramLocalPath saved to local path after download *@return*/publicstaticboolean downfile (String URL,intport,string Username, string password, string remotepath,string filename,string LocalPath) { BooleanSuccess =false; FtpClient FTP=Newftpclient (); Try { intreply; Ftp.connect (URL, port); //If you use the default port, you can connect directly to the FTP server using Ftp.connect (URL)Ftp.login (username, password);//LoginReply =Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply)) {Ftp.disconnect (); returnsuccess; } ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directoryftpfile[] fs =Ftp.listfiles (); for(Ftpfile ff:fs) {if(Ff.getname (). Equals (FileName)) {File LocalFile=NewFile (localpath+ "/" +ff.getname ()); OutputStream is=NewFileOutputStream (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) {}}} returnsuccess;}
Source: http://www.cnblogs.com/langtianya/archive/2013/02/01/2889682.html
Using FtpClient in Java to upload files for download