Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Org.apache.commons.io.IOUtils;
Import org.apache.commons.net.ftp.FTPClient;
Import Org.apache.commons.net.ftp.FTPFile; /** * * "Function Description: FTP Tool class" * "features Detailed description: Detailed description of the function" * @author "lfssay" * @see "Related class/method" * @version "class version number, 2013-8-19 afternoon 4:3 9:37 "* @since" product/Module Version "*/public class Ftputils {public static void main (string[] args) {//Boole
An flag = FileUpload ("E:/tt/data", "/admin/");
Boolean flag = FileDownload ("e:/tt/te/128", "/admin/748/", "748");
SYSTEM.OUT.PRINTLN (flag);
Boolean flag = Deletedir ("/admin/3128/");
Boolean flag = DeleteFile ("/admin/2616/input/a0012.xml");
SYSTEM.OUT.PRINTLN (flag);
/** * * "Feature description: Get FTP Landing Results ftpclient" * "features detailed description: Feature detailed description" * @see "Class, Class # method, Class # member" * @return * @throws IOException */public static ftpclient Getftpclient () throws ioexception{ftpclient ftpclient = new FtpClient ();
Ftpclient.connect (Configreader.get ("Ftp.server", ""));
Boolean flag = Ftpclient.login (Configreader.get ("Ftp.username", ""), Configreader.get ("Ftp.pwd", ""));
if (flag) {Ftpclient.setcontrolkeepalivetimeout (Configreader.getint ("Ftp.active.time", 600));
return ftpclient;
}else{return null; }/** * * "feature description: Upload files and folders" * "features detailed description: Feature detailed description" * @see "Class, Class # method, Class # member" * @param file
Path to upload file @param ftprepath The relative position of the store after upload eg:/admin/pic/* @return True, only returns true if all processes are successful, not true indicates failure
*/@SuppressWarnings ("finally") public static Boolean fileupload (String filepath,string ftprepath) {
FtpClient ftpclient = new FtpClient ();
try {ftpclient = Getftpclient ();
Ftpclient.setbuffersize (1024);
Ftpclient.setcontrolencoding ("GBK");
After using the iterator method, only a ftpclient connection is established to facilitate the secure Iterateupload (Ftpclient,filepath,ftprepath) of the FTP performance;
catch (IOException e) {e.printstacktrace ();
throw new RuntimeException ("FTP upload failed", e);
Finally {try {ftpclient.disconnect ();
catch (IOException e) {e.printstacktrace ();
throw new RuntimeException ("Turn off FTP exception", e);
Returns true after all processes have succeeded; }/** * * "Feature Description: Upload Tool class, Iteration" * "feature detailed Description: Feature detailed description" * @see "Class, Class # method, Class # member" * @param ftpclie NT * @param filePath * @param ftprepath * @throws ioexception/public static void Iterateupload (
FtpClient ftpclient,string filepath,string ftprepath) throws ioexception{file Srcfile = new file (FilePath); Original upload file if (Srcfile.isfile ()) {FileInputStream FIS = new FileInputStream (Srcfile);
Ftpclient.changeworkingdirectory (Ftprepath);
Ftpclient.setfiletype (Ftpclient.binary_file_type);
Ftpclient.storefile (Srcfile.getname (), FIS);
}else if (srcfile.isdirectory ()) {//As long as it is a folder, create the address of the current folder.
Ftpclient.makedirectory (Ftprepath+file.separator+srcfile.getname ());
file[] files = srcfile.listfiles (); If empty, only the folder is created for (File f:files) {iterateupload (Ftpclient,f.getabsolutepath (), Ftprepath+file.
Separator+srcfile.getname ()); }}/** * * "feature Description: FTP download, specify download FTP on a directory or file to the local" * "feature detailed Description: Feature detailed description" * @see "Class, Class # method, Class #成员 "* @param downloadpath Local address * @param dir ftp file address (relative such as:/admin/rt/) * @param filename ftp to download * @return true, only if all processes are successful returns True, not true indicates failure/@SuppressWarnings ("finally") public static Boolean file Download (String downloadpath,string dir,string fileName){FtpClient ftpclient = new FtpClient ();
try {ftpclient = Getftpclient ();
Ftpclient.setbuffersize (1024);
Ftpclient.setfiletype (Ftpclient.binary_file_type);
Iteratedown (Ftpclient,dir,downloadpath+file.separator+filename);
catch (IOException e) {e.printstacktrace ();
throw new RuntimeException ("FTP download Failed", e);
Finally {try {ftpclient.disconnect ();
catch (IOException e) {e.printstacktrace ();
throw new RuntimeException ("Shutdown FTP connection failed", e);
return true; }/** * * "feature description: Download function iterator," * "feature detailed description: Feature Details" * @see "Class, Class # method, Class # member" * @param FtpClient ftpclient connector * @param dir FTP path * @param downloadpath Local Path * @throws IOException/P ublic static void Iteratedown (FtpClient ftpclient,string dir,string DowNloadpath) throws ioexception{//list Whether this address corresponds to a folder or file ftpfile[] files = ftpclient.listfiles (dir);
for (Ftpfile f:files) {//If the current directory has not yet been created, then create file Filedown = new file (Downloadpath) here;
if (!filedown.exists ()) {filedown.mkdirs ();
String localpath = Downloadpath+file.separator+f.getname ();
File File = new file (LocalPath);
if (F.isfile ()) {FileOutputStream FOS = null;
FOS = new FileOutputStream (localpath);
String path = Dir+file.separator+f.getname ();
Ftpclient.retrievefile (path, FOS);
ioutils.closequietly (FOS);
}else if (f.isdirectory ()) {file.mkdirs ();
Iteratedown (Ftpclient,dir+file.separator+f.getname (), localpath); }}/** * * "Feature Description: Delete the file specified on FTP" * "features detailed description: Feature detailed description" * @see "Class, Class # method, Class #Member "* @param file path on ftppath FTP * @return True succeeded, false, failed/public static Boolean Deletedir (String FTP
Path) {ftpclient ftpclient = new FtpClient ();
Boolean flag = false;
try {ftpclient = Getftpclient ();
Ftpclient.setcontrolkeepalivetimeout (Configreader.getint ("Ftp.active.time", 600));
Ftpclient.setfiletype (Ftpclient.binary_file_type);
Flag = Iteratedelete (Ftpclient,ftppath);
Ftpclient.disconnect ();
The catch (IOException e) {//TODO exception handling block E.printstacktrace ();
return flag; /** * * "Feature Description: Delete Folder" * "features detailed description: Feature detailed description" * @see "Class, Class # method, Class # member" * @param ftpclient * @param ftppath folder's address * @return True table appears successful, False failed * @throws IOException/public static Boolean ite Ratedelete (ftpclient ftpclient,string ftppath) throws ioexception{ftpfile[] files = ftpclient.listfiles (FtpPath);
Boolean flag = false;
for (Ftpfile f:files) {String path = Ftppath+file.separator+f.getname ();
if (F.isfile ()) {//is the file to delete the file Ftpclient.deletefile (path);
}else if (f.isdirectory ()) {iteratedelete (Ftpclient,path);
}///Every time you delete the folder, check to see if there are any files under it, delete the empty folder ftpfile[] Files2 = Ftpclient.listfiles (Ftppath);
if (files2.length==0) {flag = Ftpclient.removedirectory (Ftppath);
}else{flag = false;
return flag; /** * * "feature Description: Delete File" * "features Detailed Description: Features detailed description" * @see "Class, Class # method, Class # member" * @param filePath *
return */public static Boolean DeleteFile (String FilePath) {Boolean flag = false;
try {ftpclient ftpclient = getftpclient ();
Flag = Ftpclient.deletefile (FilePath);
catch (IOException e) {//TODO exception handling block E.printstacktrace ();
return flag;
}
}
The above is transferred from: http://blog.csdn.net/linfssay/article/details/10117101