Based on the Apache FTP implementation file upload download tool, upload files need to consider the following issues (example is the continuation of the function):
(1) Whether there is a directory for the FTP server, if the directory does not exist, you need to create a directory.
(2), to determine whether the upload file already exists, if there is a need to delete before uploading or continued transmission.
1, upload or download the status of the enumeration class:
Package com.scengine.wtms.utils.ftp;
Public enum Uploadstatus
{
file_exits (0), create_directory_success (1), Create_directory_fail (2), Upload_ From_break_success (3), Upload_from_break_faild (4), download_from_break_success (5), Download_from_break_faild (6), Upload_new_file_success (7), upload_new_file_failed (8), delete_remote_success (9), Delete_remote_faild (a), Remote_ Bigger_local (one), Remote_smaller_locall (a);
private int status;
public int GetStatus ()
{return
status;
}
public void setstatus (int status)
{
this.status = status;
}
Uploadstatus (int status)
{
this.status = status;
}
}
2, Tool class code:
Package com.scengine.wtms.utils.ftp;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Org.apache.commons.net.PrintCommandListener;
Import Org.apache.commons.net.ftp.FTP;
Import org.apache.commons.net.ftp.FTPClient;
Import Org.apache.commons.net.ftp.FTPFile;
Import org.apache.commons.net.ftp.FTPReply;
public class Continueftp {private FtpClient ftpclient = new FtpClient (); The/** * Object construction setting outputs the commands used in the procedure to the console/public continueftp () {This.ftpClient.addProtocolCommandListener (new P)
Rintcommandlistener (New PrintWriter (System.out));
/** * * * * java Programming for connecting to FTP server * * @param hostname * Host name * * @param port *
* * @param username * User name * * @param password * Password * * * @return Whether the connection succeeded * * @throws IOEXCEPtion */public boolean connect (string hostname, int port, string Username, string password) throws IOException
{Ftpclient.connect (hostname, port);
if (Ftpreply.ispositivecompletion (Ftpclient.getreplycode ())) {if (Ftpclient.login (username, password))
{return true;
} disconnect ();
return false;
/** * * Download files from FTP server * * @param remote file path * * @param local file path * * @return Success * * @throws ioexception * * @SuppressWarnings ("resource") public boolean Down
Load (string remote, String local) throws IOException {Ftpclient.enterlocalpassivemode ();
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");
return false; } Long lremotesize = Files[0].getsize ();
if (f.exists ()) {OutputStream out = new FileOutputStream (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");
return false;
} ftpclient.setrestartoffset (F.length ());
result = Ftpclient.retrievefile (remote, out);
Out.close ();
else {outputstream out = new FileOutputStream (f);
result = Ftpclient.retrievefile (remote, out);
Out.close ();
return result; /** * * * * Upload files to FTP server, support breakpoint Continuation * * @param local * name, absolute path * * @param remote * Remote file path, using/home/directory1/subdirectory/file.ext * According to the path specified on Linux, support multilevel directory nesting, support recursively create nonexistent directory structure * * @retur N Upload Results * * @throws IOException/@SuppressWarnings ("resource") public uploadstatus upload (String loca L, String Remote) throws IOException {//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 eye
Record does not exist, then recursively creates the 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 (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 whether there is a remote 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) {return uploadstatus.file_exits; else if (Remotesize > Localsize) {return uploadstatus.remote_bigger_local;
//Attempt to move the read pointer within the file to implement the breakpoint InputStream is = new FileInputStream (f);
if (Is.skip (remotesize) = = remotesize) {ftpclient.setrestartoffset (remotesize);
if (Ftpclient.storefile (remote, is)) {return uploadstatus.upload_from_break_success;
}//If the breakpoint is not successful, delete the file on the server and upload if (!ftpclient.deletefile (Remotefilename)) {
return uploadstatus.delete_remote_faild;
is = new FileInputStream (f);
if (Ftpclient.storefile (remote, is)} {result = Uploadstatus.upload_new_file_success;
else {result = uploadstatus.upload_new_file_failed;
} is.close ();
else {InputStream is = new FileInputStream (local);
if (Ftpclient.storefile (Remotefilename, is)} {result = Uploadstatus.upload_new_file_success;
} else { result = uploadstatus.upload_new_file_failed;
} is.close ();
return result;
/** * * Disconnect from remote server * * @throws ioexception/public void disconnect () throws IOException
{if (ftpclient.isconnected ()) {ftpclient.disconnect ();
} public static void Main (string[] args) {continueftp myftp = new Continueftp ();
try {myftp.connect ("192.168.1.200", "Duser", "HTPDuserXP32"); System.out.println (Myftp.upload ("C:\\users\\administrator\\desktop\\swing.drawer.jar", "/jars/swing.drawer.jar")
));
Myftp.disconnect ();
catch (IOException e) {System.out.println ("Connection FTP error:" + e.getmessage ()); }
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.