FTP Tool Class

Source: Internet
Author: User
Tags ftp login

 PackageCom.ttg.utils.common;//~---non-jdk imports--------------------------------------------------------Importorg.apache.commons.net.ftp.FTPClient;ImportOrg.apache.commons.net.ftp.FTPClientConfig;ImportOrg.apache.commons.net.ftp.FTPFile;Importorg.apache.commons.net.ftp.FTPReply;ImportJava.io.*;//~---JDK imports------------------------------------------------------------//~---classes----------------------------------------------------------------/** *  */ Public classFtpclientutil {/*** Field FTP Description*/    Private Staticftpclient FTP; /**     *     * @paramPath * uploaded to the FTP server under which paths *@paramAddr * Address *@paramPort * Port number *@paramUsername * User name *@paramPassword * Password *@return     * @throwsException*/     Public Static BooleanConnect (string path, String addr,intPort, string Username, string password)throwsException {Booleanresult =false; FTP=Newftpclient (); intreply;    Ftp.connect (addr, port);    Ftp.login (username, password);    Ftp.setfiletype (Ftpclient.binary_file_type); Reply=Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply))        {Ftp.disconnect (); returnresult;    } ftp.changeworkingdirectory (path); Result=true; returnresult; }    /**     *     * @paramfile * uploaded files or folders *@throwsException*/     Public Static voidUpload (file file)throwsException {if(File.isdirectory ()) {ftp.makedirectory (File.getname ());        Ftp.changeworkingdirectory (File.getname ()); string[] Files=file.list ();  for(inti = 0; i < files.length; i++) {File file1=NewFile (File.getpath () + "\ \" +Files[i]); if(File1.isdirectory ()) {upload (file1);        Ftp.changetoparentdirectory (); } Else{File file2=NewFile (File.getpath () + "\ \" +Files[i]); FileInputStream input=NewFileInputStream (file2);            Ftp.storefile (File2.getname (), input);        Input.close (); }        }    } Else{File file2=NewFile (File.getpath ()); FileInputStream input=NewFileInputStream (file2);        Ftp.storefile (File2.getname (), input);    Input.close (); }    }    /*** Description: Uploading files to FTP server * *@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 file name on 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; FTP=Newftpclient (); Ftpclientconfig Config=NewFtpclientconfig (); //config.setxxx (YYY);//Change Required Options//For example Config.setservertimezoneid ("Pacific/pitcairn")Ftp.setdatatimeout (6000); Ftp.setconnecttimeout (6000);    Ftp.configure (config); Try {        intreply; Ftp.connect (URL, port); //connecting to an FTP server//ftp.connect (URL); //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: Download files from FTP server * *@paramURL * FTP server hostname *@paramPort * FTP server ports *@paramUsername * FTP Login account *@paramPassword * FTP login password *@paramRemotePath * Relative path on FTP server *@paramfilename * file name to download *@paramLocalPath * Save to Local path after download *@return     */     Public Static BooleanDownfile (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; }}

FTP Tool Class

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.