package **.tool;
import org.apache.commons.net.ftp.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class FTPHelper {
private FTPClient ftp = null;
private String server;
private String uname;
private String password;
private int port = 21;
public FTPHelper(String server, int port, String uname, String password) {
this.server = server;
if (this.port > 0) {
this.port = port;
}
this.uname = uname;
this.password = password;
this.ftp = new FTPClient();
}
public FTPClient connectFTPServer() throws Exception {
try {
this.ftp.setControlEncoding("GBK");
this.ftp.setAutodetectUTF8(true);
this.ftp.connect(this.server, this.port);
if (!this.ftp.login(this.uname, this.password)) {
this.ftp.logout();
this.ftp = null;
return this.ftp;
}
this.ftp.setFileType(2);
this.ftp.enterLocalPassiveMode();
this.ftp.setConnectTimeout(10000);
this.ftp.setBufferSize(1024);
int replyCode = this.ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
closeFTPClient();
this.ftp = null;
throw new Exception( this.server + " - "+ this.uname + " - " + this.password);
}
return this.ftp;
}catch (Exception e) {
this.ftp.disconnect();
this.ftp = null;
throw e;
}
}
public FTPClientConfig getFTPClientConfig() throws Exception {
String systemKey = "UNIX";
String serverLanguageCode = "zh";
FTPClientConfig conf = new FTPClientConfig(systemKey);
conf.setServerLanguageCode(serverLanguageCode);
conf.setDefaultDateFormatStr("yyyy-MM-dd");
return conf;
}
public Boolean uploadFile(String localFile, String newName) throws Exception {
InputStream input = null;
boolean success = false;
try {
File file = null;
if (checkFileExist(localFile)) {
file = new File(localFile);
}
input = new FileInputStream(file);
success = this.ftp.storeFile(newName, input);
if (!success) {
throw new Exception("");
}
} catch (Exception e) {
throw e;
} finally {
if (input != null) {
input.close();
}
}
return Boolean.valueOf(success);
}
public Boolean uploadFile(InputStream input, String newName) throws Exception {
boolean success = false;
try {
success = this.ftp.storeFile(newName, input);
if (!success) {
throw new Exception("");
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (input != null) {
input.close();
}
}
return Boolean.valueOf(success);
}
public Boolean uploadFile(String localFile, String newName, String remoteFoldPath) throws Exception {
InputStream input = null;
boolean success = false;
try {
File file = null;
if (checkFileExist(localFile)) {
file = new File(localFile);
}
input = new FileInputStream(file);
if (!changeDirectory(remoteFoldPath).booleanValue()) {
System.out.println("");
return Boolean.valueOf(false);
}
success = this.ftp.storeFile(newName, input);
if (!success) {
throw new Exception("");
}
} catch (Exception e) {
throw e;
} finally {
if (input != null) {
input.close();
}
}
return Boolean.valueOf(success);
FTP处理类
于 2022-08-30 09:22:46 首次发布
本文档详细介绍了如何使用FTP进行文件下载和上传的步骤,涵盖了FTP在服务器管理中的基本应用,尤其在Linux和Apache环境下的实践操作。
摘要由CSDN通过智能技术生成