EDTFTPJ let Java upload ftp file support breakpoint continue to pass

Source: Internet
Author: User
Tags ftp connection ftp file ftp upload file ftp port number

In the Java implementation of the FTP upload file function, especially when uploading large files, you can need such a function: The program in the process of uploading accidentally terminated, the file passed more than half, want to go from the place to continue transmission; or want to do similar thunder download similar features, the file is too big, today half, sleep first, Continue to preach tomorrow.

Java uploads FTP files, using more tools is Apache commons-net. If you want to use commons-net to achieve the FTP upload breakpoint continued to be a bit of trouble.

In addition to commons-net, there are a lot of very good FTP tools, here use EDTFTPJ This tool to achieve the continuation of the breakpoint.

Download: http://www.enterprisedt.com/


Download the free version here, the relatively charged version of a few features, but enough to use.

Download complete, unzip, under the Lib folder, you can see the Edtftpj.jar file. Put it in the project.

Here is the code for the FTP breakpoint to continue uploading the file:

ImportJava.io.File; Importjava.io.IOException; Importcom.enterprisedt.net.ftp.FTPException; ImportCom.enterprisedt.net.ftp.FTPTransferType; Importcom.enterprisedt.net.ftp.FileTransferClient; ImportCom.enterprisedt.net.ftp.WriteMode;  Public classFtptool {/*** Upload tool for EDTPTPJ*/      Privatefiletransferclient FTP; /*** FTP IP*/      PrivateString IP; /*** FTP port number*/      Private intPort; /*** FTP user name*/      PrivateString username; /*** FTP Password*/      PrivateString password; /*** * Construction method, initialize FTP IP, FTP port, FTP username, ftp password *@paramIP * FTP IP *@paramPort * FTP ports *@paramUsername * FTP User name *@paramPassword * FTP password*/       PublicFtptool (String IP,intport, string Username, string password) {           This. IP =IP;  This. Port =Port;  This. Username =username;  This. Password =password; }        /*** * Connect FTP * *@throwsftpexception * ftpexception *@throwsIOException * IOException *@authorXXG*/       Public voidConnect ()throwsftpexception, IOException {FTP=Newfiletransferclient ();          Ftp.setremotehost (IP);          Ftp.setremoteport (port);          Ftp.setusername (username);            Ftp.setpassword (password); //set binary mode uploadFtp.setcontenttype (ftptransfertype.binary);      Ftp.connect (); }        /*** * upload Local files to the FTP server with the same file name as the original file name * *@paramlocalfile * Local file path *@paramRemotefilepath * uploaded to the directory where the FTP server is located (this directory must already exist) *@throwsIOException * IOException *@throwsftpexception * ftpexception *@authorXXG **/       Public voidresumeupload (String localfile, string remotefilepath)throwsftpexception, IOException {File local=NewFile (LocalFile); //ftp file full pathString Remotefilefullpath = Remotefilepath +Local.getname (); //upload: Writemode.resume means the breakpoint continues to passftp.uploadfile (LocalFile, Remotefilefullpath, Writemode.resume); }        /*** * Close FTP connection * *@throwsIOException * IOException *@throwsftpexception * ftpexception *@authorXXG*/       Public voidClose ()throwsftpexception, IOException {ftp.disconnect (); }  }  

Here is the main method to test the continuation of the breakpoint (the test can be uploaded in the middle of the process to close the program, and then start uploading, to see if it is continued):

 Public Static voidMain (string[] args) {Ftptool Ftptool=NewFtptool ("192.168.7.49", "editor", "Tvm_editor"); Try{ftptool.connect (); Try{System.out.println ("Start uploading files ..."); Ftptool.resumeupload ("E:\\ software \\eclipse-jee-indigo-SR2-win32.zip", "wucao/aa/"); System.out.println ("Complete the upload file. "); }          Catch(Exception e) {e.printstacktrace (); }          finally{ftptool.close (); }      }      Catch(Exception e) {e.printstacktrace (); }  }  

In the above code, Ftp.uploadfile (Localfile,remotefilefullpath, Writemode.resume) is the third parameter in the code writemode.resume That means that the breakpoint continues to pass.

In addition to the writemode.resume way of uploading, there are two other ways:

writemode.overwrite: Overwrite upload, is the last time if not passed, this time delete the last retransmission.

writemode.append: Continuation, that is, if there is a file with the same name on the FTP server, then it is added later.

Note Continuation and breakpoint continuation of the distinction: for example, there is a 100M file upload, the last time the 80M, then the resume way, the rest of the 20M finished, but for append, it will be in 80M after the re-transmission, the final success of the FTP file size is 180M.

Finally, note that: not all FTP servers support the continuation of the breakpoint, the server with the continuation of the breakpoint can also turn off the function of the continuation of the breakpoint, a server that does not support the continuation of the breakpoint will throw an exception with resume. Therefore, before using the FTP breakpoint to continue the transmission, the first to confirm whether the FTP server itself supports the continuation of the breakpoint.

This article transferred from: http://blog.csdn.net/kazeik/article/details/8260067

EDTFTPJ let Java upload ftp file support breakpoint continue to pass

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.