To upload a file to an FTP server:
/*** Description: Uploading files to FTP server *@paramURL FTP server hostname *@paramPort FTP server ports, if default port please write-1 *@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; //connecting to an FTP server if(Port >-1) {ftp.connect (URL, port); } Else{ftp.connect (URL); } //log in to FTPftp.login (username, password); Reply=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) {Success=false; Logger.error (Exception_name, E); } finally { if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException e) {logger.error (Exception_name, E); } } } returnsuccess; }
To download files from an FTP server:
/*** 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 */ Public Static BooleanDownloadFile (String URL,intport, string Username, string password, string remotepath, String fileName, String localPath) { BooleanSuccess =false; FtpClient FTP=Newftpclient (); Try { intreply; //connecting to an FTP server if(Port >-1) {ftp.connect (URL, port); } Else{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) {logger.error (Exception_name, E); } finally { if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException e) {logger.error (Exception_name, E); } } } returnsuccess; }
To delete a file on the FTP server:
/*** < Delete files on FTP > * < remotely delete audio files on FTP server > *@paramURL FTP Server IP address *@paramPort FTP Server ports *@paramUsername FTP Server logon name *@paramPassword FTP server password *@paramRemotePath Remote file path *@paramfilename to delete *@return * @see[Class, Class # method, Class # member]*/ Public Static BooleanDeleteftpfile (String URL,intport, string Username, string password, string remotepath, String fileName) { BooleanSuccess =false; FtpClient FTP=Newftpclient (); Try { intreply; //connecting to an FTP server if(Port >-1) {ftp.connect (URL, port); } Else{ftp.connect (URL); } //Loginftp.login (username, password); Reply=Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply)) {Ftp.disconnect (); returnsuccess; } //Transfer to FTP server directoryftp.changeworkingdirectory (RemotePath); Success= Ftp.deletefile (RemotePath + "/" +fileName); Ftp.logout (); } Catch(IOException e) {logger.error (Exception_name, E); Success=false; } finally { if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException e) {logger.error (Exception_name, E); } } } returnsuccess; }
Required JAR Packages:
Commons-net-2.0.jar
This article transferred from: http://wenkaixuan.iteye.com/blog/1739344
Java operation FTP, to achieve file upload download delete operation