The example of this article tells the method of Java's implementation of FTP breakpoints. Share it for everyone for your reference. The specific analysis is as follows:
Here we mainly use the net package in Apache. URL http://commons.apache.org/net/. For the download and API documentation of the specific package, please see the official website. The upload of the breakpoint is to set the starting position of the transmission during the upload. And set binary transmission.
Import java.io.file; Import Java.FileInputStream; Import java.io.FileoutPutstream; Import java.io.ioxception; Import java.inputStream; Import t java.io.outputstream; Import java.io.printwriter; Import Org.apache.commons.net.printCommandListener; Import org.apache.commons.netp.ftp; Import org.apache.net.ftpClient; Import Chemons.net.ftp.ftpfile; Import ORG.APACHE.COMMONS.NET.FTP.FTPREPLY; Public Class Continueftp {Private ftpclient ftpclient = new ftpclient (); The command output to the console THIS.FTPClient.addProtocolCommandListener ( New PrintCommandListener (New Printwriter (System.out));} /*** Java programming is used to connect to the FTP server* @Param Hostname Host* @Param Port port* @param username user name* @ Param Password password* Whether the connection of @Return is successful * @throws IOEXception */ Public Boolean Connect (String HostName, Int Port, String UserName, String Password) Throws IOEXCEPTION {ftpClien T.Connect (hostname, port); if (ftpreply.ispositiveCompleTion (ftpClient.getreplycode () )) {if (ftpclient.login (username, password)) {Return True;}} Disconnect (); Return false;} /*** download files from the ftp server* @param remote remote file path* @Param Local File path* @Return is successful* @Throws IOEXception*/ Public Boolean Download (String Remote, String Local) Throws IOEXCEPTION ODE (); ftpclient.setFiletype (ftp.binary_file_type); Boolean Result; File F = New File ( local); ftpfile [] files = ftpclient.listFiles (remote); if (files.Length! = 1) {system.out.println ("remote file is not unique"); ZE = FILES [0] .getsize (); if (f.exist ()) {outputStream out = new fileoutstream (F, TRUE); system.out.println ("local file size is:"+f.LENGTH ())); if (f. length ()> = lremotesize) {System.out.println ("Local file size is greater than the size of the remote file, download suspension"); return; Client.retrieveful (remote Out); out.Close ();} else {outputStream out = new fileoutPutstream (f); result = ftpClient.retrieveFile (remote, out); out.Close ();} rn result;} /*** upload file Go to the FTP server and support breakpoints* @Param Local local file name, absolute path* @param remote remote file path, use /home/directory1/subdirectory/file.ext specifications according to the path on the Linux, support multi -level directory. Nesting, supporting recursive creation of the unprecedented directory structure* @Return Upload results* @throws IOEXCEPTION*/PUBLIC UploadStatus Upload (String Local, String Remote) Throws IOEXCEPTION {// Set p. assivemode transmission ftpclient.enterLocalPassiveMode (); // Set The way to transmit ftpclient.setFiletype (ftp.binary_file_type); uploadStatus result; // The processing of remote directory processing string remoteFilename; if (Remote.Contains ("// ")) {remoteFilename = remote.substring (remote.lastindexof ("/")+1); String Directory = Remote.substring (0, Remote.lastindexof ("/"); if (! Directory.equalSignorecase ("/") Directory (Directory)) {// If the remote directory does not exist, recursively create remote server directory int Start = 0; int End = 0; if (Directory.startswith ("/") {start = 1;} else {start = 0;} END = Directory.indexof ("/", Start); While (TRUE) {String Subdirectory = Remote.Substring (Start, END); if (! Ftpclient.changeworkingDirectory )) {if (ftpclient.makedirectory (subdirectory)) { ftpclient.changeworkingDirectory (subdirectory);} else {System.out.println ("Creation Directory Failure"); Return uploadStatus.crectory_fail; nd + 1; End = Directory.indexof (" /", Start); / /Check whether all directory is created if (end <= start) {break;}}} // Check if there is a file ftpfile [] files = ftpclient.listFiles (remoteFilename); if (files.length == 1) long remoteSize = files [0] .getsize (); file f = new file (local); long localSize = f.length (); if (remotesize == localSize) {returnStatu s.file_exits;} Else if (Remotesize> LocalSize ) {RETURN UPLOADSTAS.RMOTE_BIGGER_LOCAL;} // Try to read the pointer in the mobile file to achieve the breakpoint of inputStream is = New FileInputStream (f); if (IS.SKIP (Remotesize) == Remo TESIZE) {ftpclient.Setresttartoffset (Remotesize );; ftpclient.Storefile (Remote, is)) {Return uploadStatus.upload_from_break_success; EFILE (remoteFilename) { Return uploadStatus.delete_remote_fail;} is = New FileInputStream (F); if (ftpClient.storefile (Remote, is)) {result = UPLOADSTAS.UPLOAD_NEW_NEW _File_success;} else {result = uploadStatus.upload_new_file_faled;} is.close ();} else {InputStream is = New FileInputStream (Local); if (ftpclient.Storefile (RemoteFilename, is)) {result = uploadStatus.upload_new_success; lse {result = uploadStatus.upload_new_file_faird;} is.close ();} Return Result;} / ** * Connect to disconnect with the remote server * @throws IOEXception */ Public Void Disconnect () Throws IOEXCEPTION {if (ftpClient.isconnect ()) {ftpClient.disConnect (). ;}} Public Static Void Main (String [] ARGS ) {Continueftp myftp = New Continueftp (); Try {myftp.connect Then, then flv ","/mis/video/vp6.flv "); myftp.disconnect ();} Catch (IOEXCEPTION E) {System.out.println (" Connect FTP error: "+e.getMessage ()));} }}It is hoped that this article is helpful to everyone's Java program design.