packagecc.julong.cash.tools;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.Vector;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;importorg.apache.commons.net.ftp.FTPSClient;/*** FTP客户端,支持FTPS协议
*
* 例:
* FtpClient client = new FtpClient(true); // 功能:创建FTP客户端,参数:【是否开启ftps】
* client.connect("192.168.0.1", "root", "root", 990); // 功能:连接ftp服务器,参数:【连接地址】【用户名】【密码】【端口号】
* System.out.println(client.listFileInDir("/")); // 功能:列出主目录下文件列表,参数:【服务器路径】
* System.out.println(client.downloadFile("/123.txt","D:/123.txt")); //功能:下载文件,参数:【服务器路径】【本地路径】
* System.out.println(client.uploadFile("D:/py_work/test.py", "/test.py")); // 功能:上传文件,参数:【本地路径】【服务器路径】
*
*@authorray
**/
public classFtpClient{privateFTPClient client;public FtpClient(booleanftps) {if(ftps) {try{
client= new FTPSClient(true);
}catch(Exception e) {
e.printStackTrace();
}
}else{
client= newFTPClient();
}
}public boolean changeDir(String remotePath) throws Exception {//路径
returnclient.changeWorkingDirectory(remotePath);
}public boolean connect(String host, String login, String password, int port) throws Exception {//ftp 链接
client.connect(host, port);int reply =client.getReplyCode();if(FTPReply.isPositiveCompletion(reply)) {if(client.login(login, password)) {
client.enterLocalPassiveMode();return true;
}
}
disconnect();return false;
}public void disconnect() throwsException {
client.disconnect();
}public boolean downloadFileAfterCheck(String remotePath, String localFile) throwsIOException {booleanrst;
FileOutputStream out= null;try{
File file= newFile(localFile);if (!file.exists()) {
out= newFileOutputStream(localFile);
rst=client.retrieveFile(remotePath, out);
}else{
rst= true;
}
}finally{if (out != null) {
out.close();
}
}if (out != null) {
out.close();
}returnrst;
}public boolean downloadFile(String remotePath, String localFile) throwsIOException {booleanrst;
FileOutputStream out= null;try{
out= newFileOutputStream(localFile);
rst=client.retrieveFile(remotePath, out);
}finally{if (out != null) {
out.close();
}
}returnrst;
}public Vector listFileInDir(String remoteDir) throwsException {if(changeDir(remoteDir)) {
FTPFile[] files=client.listFiles();
Vector v = new Vector();for(FTPFile file : files) {if (!file.isDirectory()) {
v.addElement(file.getName());
}
}returnv;
}else{return null;
}
}public boolean uploadFile(String localFile, String remotePath) throwsIOException {
FileInputStream in= newFileInputStream(localFile);booleanrst;try{
rst=client.storeFile(remotePath, in);
}finally{
in.close();
}returnrst;
}public Vector listSubDirInDir(String remoteDir) throwsException {if(changeDir(remoteDir)) {
FTPFile[] files=client.listFiles();
Vector v = new Vector();for(FTPFile file : files) {if(file.isDirectory()) {
v.addElement(file.getName());
}
}returnv;
}else{return null;
}
}public booleancreateDirectory(String dirName) {try{returnclient.makeDirectory(dirName);
}catch(IOException e) {
e.printStackTrace();
}return false;
}public booleanisARemoteDirectory(String path) {
String cache= "/";try{
cache=client.printWorkingDirectory();
}catch(NullPointerException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}try{boolean isDir =changeDir(path);
changeDir(cache);returnisDir;
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}return false;
}publicString getWorkingDirectory() {try{returnclient.printWorkingDirectory();
}catch(IOException e) {
e.printStackTrace();
}return null;
}/*** 执行指定命令
*
*@paramcommandAndParams
* 要执行的命令以及命令的参数
*@returnString[] 执行结果*/
public VectordoCommand(String command,String params){
Vector v = new Vector();try{boolean b = client.doCommand("help","");
System.out.println(b);
String[] strs=client.doCommandAsStrings(command, params);
System.out.println(command+params);
System.out.println(strs+ "-----123");
System.out.println(client.listHelp());
strs=client.getReplyStrings();for(String str : strs) {
v.addElement(str);
}
}catch(IOException e) {
e.printStackTrace();
}returnv;
}/*** 判断文件是否存在ftp目录上
*
*@parampathAndName
* ftp上的文件所在所在文件全路径
*@return
*/
public boolean exsitsFile(String pathAndName) throwsException{
String fileName= pathAndName.substring(pathAndName.lastIndexOf("/") + 1);
String path= pathAndName.substring(0,
pathAndName.lastIndexOf("/") + 1);return this.listFileInDir(path).contains(fileName);
}
}