Use FtpClient to upload downloads in Java
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 FTP server3 * @Version1.0 4:31:09 PM by Tri Hongbao ([email protected]) Create4 * @paramURL FTP server hostname5 * @paramPort FTP Server ports6 * @paramUsername FTP login account7 * @paramPassword FTP login password8 * @paramPath FTP Server save directory9 * @paramfilename uploaded to the FTP serverTen * @paraminput Stream One * @returnSuccess returns True, otherwise false A */ - Public Static BooleanUploadFile (String URL,intport,string Username, string password, string path, string filename, InputStream input) { - BooleanSuccess =false; theFtpClient 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);//Login +Reply =Ftp.getreplycode (); A if(!ftpreply.ispositivecompletion (Reply)) { at Ftp.disconnect (); - returnsuccess; - } - ftp.changeworkingdirectory (path); - ftp.storefile (filename, input); - in input.close (); - ftp.logout (); toSuccess =true; +}Catch(IOException e) { - e.printstacktrace (); the}finally { * if(ftp.isconnected ()) { $ Try {Panax Notoginseng Ftp.disconnect (); -}Catch(IOException IoE) { the } + } A } the returnsuccess; +}<pre></pre>
Below we write two small examples:
1. Upload the local file to the FTP server with the following code:
1 @Test2 Public voidTestuploadfromdisk () {3 Try {4FileInputStream in=NewFileInputStream (NewFile ("D:/test.txt"));5 BooleanFlag = 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 }Ten}<pre></pre>
2. Generate a file on the FTP server and write a string to the file
1 @Test2 Public voidtestuploadfromstring () {3 Try {4InputStream input =NewBytearrayinputstream ("Test ftp". GetBytes ("Utf-8")));5 BooleanFlag = 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 }Ten}<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 server3 * @Version1.0 5:32:36 PM by Tri Hongbao ([email protected]) Create4 * @paramURL FTP server hostname5 * @paramPort FTP Server ports6 * @paramUsername FTP login account7 * @paramPassword FTP login password8 * @paramRemotePath relative paths on the FTP server9 * @paramfilename to downloadTen * @paramLocalPath saved to local path after download One * @return A */ - Public Static BooleanDownfile (String URL,intport,string Username, string password, string remotepath,string filename,string LocalPath) { - BooleanSuccess =false; theFtpClient 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);//Login +Reply =Ftp.getreplycode (); A if(!ftpreply.ispositivecompletion (Reply)) { at Ftp.disconnect (); - returnsuccess; - } -Ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directory -ftpfile[] fs =ftp.listfiles (); - for(Ftpfile ff:fs) { in if(Ff.getname (). Equals (FileName)) { -File LocalFile =NewFile (localpath+ "/" +ff.getname ()); to +OutputStream is =NewFileOutputStream (localfile); - Ftp.retrievefile (Ff.getname (), is); the is.close (); * } $ }Panax Notoginseng - ftp.logout (); theSuccess =true; +}Catch(IOException e) { A e.printstacktrace (); the}finally { + if(ftp.isconnected ()) { - Try { $ Ftp.disconnect (); $}Catch(IOException IoE) { - } - } the } - returnsuccess;Wuyi}
PS: Welcome reprint, Reprint please indicate source: http://www.cnblogs.com/liuyitian/p/4635302.html
Writing is not easy, inevitably there are omissions and mistakes, please also be generous, please recommend
learn a little bit more everyday code less knock a little bit
Use FtpClient to upload downloads in Java