Java operation FTP, to achieve file upload download delete operation

Source: Internet
Author: User
Tags ftp login

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.