Download the latest package from commons-net and introduce it into the project.
The following is the relevant example code:
Package com.ffcs.icity.common.util;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import org.apache.commons.net.ftp.FTPClient;
Import Org.apache.commons.net.ftp.FTPFile;
Import org.apache.commons.net.ftp.FTPHTTPClient;
Import org.apache.commons.net.ftp.FTPReply;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory; /** * ftp file Upload and download function * @author Alan * @createtime 2013-6-24 * * */public class Ftputil {private final static
Logger log = Loggerfactory.getlogger (Ftputil.class);
/** * Description: Uploading files to FTP server * * @param URL * FTP server hostname * @param port * FTP server ports * @param username * FTP login account * @param password * FTP login password * @param path * FTP Service The directory * @param filename * uploaded to the FTP server file name * @param input * lostThe inflow * @return successfully returns TRUE, otherwise returns false */public static Boolean uploadfile (string url, int port, string username, string
Password, string path, string filename, InputStream input) {//initial indicates upload failed Boolean success = false;
FtpClient ftp = NULL;
String Proxyset = System.getproperty ("Proxyset"); True indicates that there is a set agent//Create FtpClient Object if ("true". Equals (Proxyset)) {ftp = new Ftphttpclient (System.getproperty ("Http.proxy
Host "), Integer.parseint (System.getproperty (" Http.proxyport "));
else {ftp = new ftpclient ();
try {int reply;
Connect FTP server//If the default port is used, you can use the Ftp.connect (URL) way to directly connect the FTP server ftp.connect (URL, port);
Login to FTP ftp.login (username, password);
See the return value is not 230, if it is, the landing success Reply = Ftp.getreplycode (); The return value starting with 2 will be true if (!).
Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();
return success;
//Go to the specified upload directory ftp.changeworkingdirectory (path); If you have a configuration agent parameter, you need to set the following mode if ("true". Equals (Proxyset)) {Ftp.enterlocalpassivEmode ();
///upload files to the specified directory ftp.storefile (filename, input);
Turn off the input stream input.close ();
Exit FTP Ftp.logout ();
Indicates upload success success = true;
catch (IOException e) {//E.printstacktrace ();
Log.error ("In Ftputil.uploadfile has a error,proxyset is {},and e is {}", new Object[]{proxyset,e});
finally {if (ftp.isconnected ()) {try {ftp.disconnect ();
catch (IOException IoE) {}} return success; /** * Description: Download files from FTP server * * @param URL * FTP server hostname * @param port * FTP Service Service Port * @param username * FTP login account * @param password * FTP login password * @param remotepath *
Relative path on FTP server * @param filename * file name to download * @param localpath * Download and save to local path * @return */public static Boolean downfile (string url, int port, string username, string password, string RemotePath, String FileName, String localpath) {//initial indicates download failure BooleAn success = false;
FtpClient ftp = NULL;
String Proxyset = System.getproperty ("Proxyset"); True indicates that there is a set agent//Create FtpClient Object if ("true". Equals (Proxyset)) {ftp = new Ftphttpclient (System.getproperty ("Http.proxy
Host "), Integer.parseint (System.getproperty (" Http.proxyport "));
else {ftp = new ftpclient ();
try {int reply;
Connect FTP server//If the default port is used, you can use the Ftp.connect (URL) way to directly connect the FTP server ftp.connect (URL, port);
Login to FTP ftp.login (username, password);
Reply = Ftp.getreplycode (); if (!
Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();
return success;
//Go to the specified download directory ftp.changeworkingdirectory (RemotePath);
If you have a configuration agent parameter, you need to set the following mode if ("true". Equals (Proxyset)) {Ftp.enterlocalpassivemode ();
//List all files under this directory ftpfile[] fs = Ftp.listfiles ();
Iterates through all the files, finds the specified file for (Ftpfile ff:fs) {if (Ff.getname (). Equals (FileName)) {File Dir = new file (LocalPath);
if (!dir.exists ()) {if (!dir.mkdirs ()) { throw new IOException ("Create save Directory Failed");
}////Based on absolute path initialization files file LocalFile = new file (LocalPath + "/" + ff.getname ());
Output stream OutputStream is = new FileOutputStream (localfile);
Download file Ftp.retrievefile (Ff.getname (), is);
Is.close ();
}//Exit FTP ftp.logout ();
Download Success Success = TRUE;
catch (IOException e) {//E.printstacktrace ();
Log.error ("In Ftputil.downfile has a error,proxyset is {},and e is {}", new Object[]{proxyset,e});
finally {if (ftp.isconnected ()) {try {ftp.disconnect ();
catch (IOException IoE) {}} return success; public static void Main (string[] args) throws FileNotFoundException {//String URL = "42.120.41.94";//INT
Port = 21;
String username = "ftp88888888";
String password = "ftp@88888888$%^";
String path = "/";
String filename = "test.txt";
InputStream in = new FileInputStream (New File ("D://test.txt")); Long A = SystEm.currenttimemillis ();
UploadFile (Url,port,username,password,path,filename,in);
Long B = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("result is OK.")
Time is "+ (b-a));
String url = "42.121.5.31";
int port = 21;
String username = "ftp88888888";
String password = "ftp@88888888$%^";
String Path = "/m00201123";
String filename = "Test12.txt";
InputStream in = new FileInputStream (New File ("D://test.txt"));
Long a = System.currenttimemillis ();
Boolean result = UploadFile (url,port,username,password,path,filename,in);
Long B = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("result is" + result +).
Time is "+ (b-a));
String url = "42.121.5.31";
int port = 21;
String username = "ftp88888888";
String password = "ftp@88888888$%^";
String RemotePath = "/home/dmpush_disk/uploads/p88888888/reports";
String filename = "20130204_4.3-1";
String localpath = "d://temp//push//";
Long a = System.currenttimemillis (); DowNFile (Url,port,username,password,remotepath,filename,localpath);
Long B = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("result is OK.")
Time is "+ (b-a)); }
}