Package com.ailk.qw.util;
Import it.sauronsoftware.ftp4j.*;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Java.io.File;
Import java.io.IOException;
public class Ftputil {
Final static Logger log = Loggerfactory.getlogger (Ftputil.class);
public static ftpclient getftp (string ip, int port, string username, string password, string charSet) throws Ftpexception, IOException, Ftpillegalreplyexception {
FtpClient client = new FtpClient ();
Client.connect (IP, port);
Client.login (username, password);
Client.setcharset (CharSet);
Client.settype (ftpclient.type_binary);//Binary
return client;
}
/**
* Remotely Move files
*
* @param IP
* @param Port
* @param username
* @param password
* @param frompath
* @param fromfilename
* @param topath
* @param tofilename
* @return
*/
public static Boolean remotemove (string ip, int port, string username, string password, string frompath, String Fromfilena Me, String Topath, String tofilename) {
Boolean result = false;
FtpClient client = null;
Log.info ("Start the" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + Tofilename ");
try {
Client = GETFTP (IP, port, username, password, "UTF-8");
Client.changedirectory (Frompath);
Client.rename (Frompath + "/" + Fromfilename, Topath + "/" + tofilename);
Client.disconnect (TRUE);
if (client.isconnected ()) {
Client.disconnect (FALSE);
}
result = true;
} catch (Ftpexception e) {
Log.error ("Add" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + Tofilename + "exception", e);
} catch (IOException e) {
Log.error ("Add" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + Tofilename + "exception", e);
} catch (Ftpillegalreplyexception e) {
Log.error ("Add" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + Tofilename + "exception", e);
} finally {
if (client.isconnected ()) {
try {
Client.disconnect (FALSE);
} catch (IOException e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpillegalreplyexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
}
}
Log.info ("Add" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + tofilename + "result is:" + result );
return result;
}
}
/**
* Remotely Move files
*
* @param IP
* @param Port
* @param username
* @param password
* @param frompath
* @param fromfilename
* @param topath
* @param tofilename
* @param charSet
* @return
*/
public static Boolean remotemove (string ip, int port, string username, string password, string frompath, String Fromfilena Me, String Topath, String tofilename, String charSet) {
Boolean result = false;
FtpClient client = null;
Log.info ("Start the" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + Tofilename ");
try {
Client = GETFTP (IP, port, username, password, charSet);
Client.changedirectory (Frompath);
Client.rename (Frompath + "/" + Fromfilename, Topath + "/" + tofilename);
Client.disconnect (TRUE);
if (client.isconnected ()) {
Client.disconnect (FALSE);
}
result = true;
} catch (Ftpexception e) {
Log.error ("Add" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + Tofilename + "exception", e);
} catch (IOException e) {
Log.error ("Add" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + Tofilename + "exception", e);
} catch (Ftpillegalreplyexception e) {
Log.error ("Add" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + Tofilename + "exception", e);
} finally {
if (client.isconnected ()) {
try {
Client.disconnect (FALSE);
} catch (IOException e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpillegalreplyexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
}
}
Log.info ("Add" + IP + "Server directory as" + Frompath + "file named" + Fromfilename + "to" + Topath + "directory file named" + tofilename + "result is:" + result );
return result;
}
}
/**
* Download File
*
* @param IP
* @param Port
* @param username
* @param password
* @param RemotePath
* @param remotefilename
* @param LocalPath
* @param localfilename
* @return
*/
public static Boolean download (string ip, int port, string username, string password, string remotepath, String remotefile Name, String LocalPath, String localfilename) {
Boolean result = false;
FtpClient client = null;
Log.info ("Start from" + IP + "Server directory for" + remotepath + "Download file" + Remotefilename + "to native" + LocalPath + "directory file named" + LocalFilename);
try {
Client = GETFTP (IP, port, username, password, "UTF-8");
Client.changedirectory (RemotePath);
Client.download (Remotefilename, New File (LocalPath + File.separatorchar + localfilename));
Client.disconnect (TRUE);
if (client.isconnected ()) {
Client.disconnect (FALSE);
}
result = true;
} catch (Ftpexception e) {
Log.info ("from" + IP + "Server directory" + remotepath + "Download file" + Remotefilename + "to native" + LocalPath + "directory file named" + LocalFilename + "Exception occurred ! ", E);
} catch (IOException e) {
Log.info ("from" + IP + "Server directory" + remotepath + "Download file" + Remotefilename + "to native" + LocalPath + "directory file named" + LocalFilename + "Exception occurred ! ", E);
} catch (Ftpillegalreplyexception e) {
Log.info ("from" + IP + "Server directory" + remotepath + "Download file" + Remotefilename + "to native" + LocalPath + "directory file named" + LocalFilename + "Exception occurred ! ", E);
} catch (Ftpabortedexception e) {
Log.info ("from" + IP + "Server directory" + remotepath + "Download file" + Remotefilename + "to native" + LocalPath + "directory file named" + LocalFilename + "Exception occurred ! ", E);
} catch (Ftpdatatransferexception e) {
Log.info ("from" + IP + "Server directory for" + remotepath + "Download file" + Remotefilename + "to the native" + LocalPath + "directory file named" + LocalFilename + "appears different Often! ", e);
} finally {
if (client.isconnected ()) {
try {
Client.disconnect (FALSE);
} catch (IOException e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpillegalreplyexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
}
}
Log.info ("from" + IP + "Server directory" + remotepath + "Download file" + Remotefilename + "to native" + LocalPath + "directory file named" + LocalFilename + "result is: "+ result);
return result;
}
}
/**
* Upload Files
*
* @param IP
* @param Port
* @param username
* @param password
* @param RemotePath
* @param file
* @return
*/
public static Boolean upload (string ip, int port, string username, string password, string remotepath, File file, string o Ldfilename, String fileName) {
FtpClient client = null;
Boolean result = false;
try {
Log.info ("Start to" + IP + "Server directory as" + RemotePath + "upload files" + fileName);
Client = GETFTP (IP, port, username, password, "UTF-8");
Client.changedirectory (RemotePath);
Client.changedirectoryup ();
Client.changedirectory ("temp");
String Frompath = Client.currentdirectory ();
String fromfilename = oldfilename;
Client.upload (file);
Client.disconnect (TRUE);
if (client.isconnected ()) {
Client.disconnect (FALSE);
}
result = Remotemove (IP, port, username, password, frompath, Fromfilename, RemotePath, fileName);
} catch (Ftpexception e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} catch (IOException e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} catch (Ftpillegalreplyexception e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} catch (Ftpabortedexception e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} catch (Ftpdatatransferexception e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} finally {
if (client.isconnected ()) {
try {
Client.disconnect (FALSE);
} catch (IOException e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpillegalreplyexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
}
}
Log.info ("to" + IP + "Server directory for" + RemotePath + "Upload file" + fileName + "result is:" + result);
return result;
}
}
/**
* Upload Files
*
* @param IP
* @param Port
* @param username
* @param password
* @param RemotePath
* @param file
* @param charSet
* @return
*/
public static Boolean upload (string ip, int port, string username, string password, string remotepath, File file, string o Ldfilename, String fileName, String charSet) {
FtpClient client = null;
Boolean result = false;
try {
Log.info ("Start to" + IP + "Server directory as" + RemotePath + "upload files" + fileName);
Client = GETFTP (IP, port, username, password, charSet);
Client.changedirectory (RemotePath);
Client.changedirectoryup ();
Client.changedirectory ("temp");
String Frompath = Client.currentdirectory ();
String fromfilename = oldfilename;
Client.upload (file);
Client.disconnect (TRUE);
if (client.isconnected ()) {
Client.disconnect (FALSE);
}
result = Remotemove (IP, port, username, password, frompath, Fromfilename, RemotePath, FileName, CharSet);
} catch (Ftpexception e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} catch (IOException e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} catch (Ftpillegalreplyexception e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} catch (Ftpabortedexception e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} catch (Ftpdatatransferexception e) {
Log.error ("+ remotepath +" Upload file "+ fileName +" to "+ IP +" server) an exception occurred! ", e);
} finally {
if (client.isconnected ()) {
try {
Client.disconnect (FALSE);
} catch (IOException e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpillegalreplyexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
} catch (Ftpexception e) {
Log.error ("Exception occurred while closing FTP connection", e);
}
}
Log.info ("to" + IP + "Server directory for" + RemotePath + "Upload file" + fileName + "result is:" + result);
return result;
}
}
public static void Main (String args[]) throws Unsupportedencodingexception {
String IP = "10.248.12.35";
int port = 21;
String username = "SM7";
String password = "SM7";
String localpath = "/ainmc/work/cpr_att/bak";
String fileName = "APP_270_EVT_60940_CPRP_ Platform condominium _1 object alarm. xls";
String RemotePath = "/ainmc/work/cpr_att/";
String remotefilename = "App_270_evt_60940_cprp_1.xls";
Ftputil.remotemove (Ip,21,username,password,localpath,filename,remotepath,remotefilename, "GBK");
Ftputil.remotemove (Ip,21,username,password,remotepath,remotefilename,remotepath,filename, "UTF-8");
// }
public static void Main2 (String args[]) {
String filename= "APP_270_EVT_60940_CPRP_ Platform condominium _1 object alarm. xls";
String name=filename.substring (0,17) +filename.substring (Filename.lastindexof ("."));
SYSTEM.OUT.PRINTLN (name);
}
public static void Main (String args[]) {
FtpClient client = null;
try {
Client=ftputil.getftp ("10.248.12.31", "SM7", "SM7", "gb2312");
Client.changedirectory ("/interface/file/unknown");
Ftpfile[] List=client.list ("app_100*");
for (Ftpfile file:list) {
System.out.println (File.getname ());
}
} catch (Ftpexception e) {
E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.
} catch (Ftpillegalreplyexception e) {
E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.
} catch (Ftpabortedexception e) {
E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.
} catch (Ftplistparseexception e) {
E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.
} catch (Ftpdatatransferexception e) {
E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.
}
}
}
Java FTP various operations