Use Apache FTP client to implement FTP clients--------support breakpoints and Chinese files.

Source: Internet
Author: User
Tags create directory ftp client

can refer to: http://blog.csdn.net/xiaozhang0731/article/details/5669151

The main use of the net package in Apache to achieve. URL http://commons.apache.org/net/. Download the specific package and API documentation please reader network.

A breakpoint upload sets the starting position of the transmission during the upload process. and set the binary transmission. Java codePackageApache.net.test;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;ImportOrg.apache.commons.net.PrintCommandListener;ImportOrg.apache.commons.net.ftp.FTP;ImportOrg.apache.commons.net.ftp.FTPClient;ImportOrg.apache.commons.net.ftp.FTPFile;Importorg.apache.commons.net.ftp.FTPReply; Publicclasscontinueftp {PrivateFtpClient ftpclient =NewFtpClient (); PublicContinueftp () {//set to output the command used in the procedure to the console This. Ftpclient.addprotocolcommandlistener (NewPrintcommandlistener (NewPrintWriter (System.out)));        /** * Connect to FTP server * @param hostname host name * @param port * @param username username * @param password Password * @return is connected successfully * @throws IOException * * PublicBooleanConnect (String hostname,intport,string username,string Password)throwsioexception{Ftpclient.connect (hostname, port);if(Ftpreply.ispositivecompletion (Ftpclient.getreplycode ())) {if(Ftpclient.login (username, password)) { returntrue; } disconnect (); returnfalse; /** * Download files from FTP server * @param remote file path * @param local file path * @return Work * @throws IOException * * PublicBooleanDownload (String remote,string local)throwsioexception{Ftpclient.enterlocalpassivemode (); Ftpclient.setfiletype (Ftp.binary_file_type);BooleanResult File f =NewFile (local); ftpfile[] files = ftpclient.listfiles (remote);if(Files.length!= 1) {System.out.println ("Remote file is not unique"); return false; }LongLremotesize = Files[0].getsize ();if(F.exists ()) {OutputStream out =NewFileOutputStream (F,true); SYSTEM.OUT.PRINTLN ("Local File size is:" +f.length ());if(F.length () >= lremotesize) {System.out.println ("Local file size is larger than remote file size, download aborted"); returnfalse;                } ftpclient.setrestartoffset (F.length ());                result = Ftpclient.retrievefile (remote, out);            Out.close (); }Else{OutputStream out =NewFileOutputStream (f);                result = Ftpclient.retrievefile (remote, out);            Out.close (); } returnResult /** * Upload files to FTP server, support breakpoint continued * @param local file name, absolute path * @param remote file path, using/home/d        Irectory1/subdirectory/file.ext in accordance with the path specified on Linux, support multilevel directory nesting, support recursively create nonexistent directory structure * @return upload results * @throws IOException */ PublicUploadstatus Upload (String local,string remote)throwsioexception{//Set Passivemode transmission Ftpclient.enterlocalpassivemode ();            A binary stream is set to transmit Ftpclient.setfiletype (ftp.binary_file_type);            Uploadstatus result; Processing of remote directories String remotefilename = remote;if(Remote.contains ("/"))                {remotefilename = remote.substring (Remote.lastindexof ("/") +1); String directory = remote.substring (0,remote.lastindexof ("/") +1);if(!directory.equalsignorecase ("/") &&!ftpclient.changeworkingdirectory (directory)) {//If the remote directory does not exist, then recursively creates the remote server directoryintstart=0;intend = 0;if(Directory.startswith ("/"))                    {start = 1; }Else{start = 0; End = Directory.indexof ("/", start); while(true) {String subdirectory = remote.substring (start,end);if(!ftpclient.changeworkingdirectory (subdirectory)) {if(Ftpclient.makedirectory (subdirectory))                            {ftpclient.changeworkingdirectory (subdirectory); }Else{System.out.println ("Create directory Failed"); return UploadStatus.Create_Directory_Fail;                             }                         }                                                  start = end + 1;                         end = directory.indexof ("/", start);                                                 //Check that all directories are created                          if(End <= start) { Break; Check to see if there is a remote file ftpfile[] files = Ftpc Lient.listfiles (Remotefilename);if(files.length = 1) {LongRemotesize = Files[0].getsize (); File f =NewFile (local);LongLocalsize = F.length ();if(remotesize==localsize) { returnUploadstatus.file_exits; }Elseif(Remotesize > Localsize) { returnuploadstatus.remote_bigger_local; //Attempt to move the read pointer within the file to implement the breakpoint InputStream is =NewFileInputStream (f);if(Is.skip (remotesize) ==remotesize) {Ftpclient.setrestartoffset (remotesize);if(Ftpclient.storefile (remote, is)) { return UploadStatus.Upload_From_Break_Success;                    }                }                                  //If the breakpoint continues to fail, delete the file on the server, upload the    again

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.