1.首先在evn.properties文件里配置ftp
#ftp服务器 地址
FTP_SERVER_NAME=xxxxx
#ftp 服务器 端口号
FTP_SERVER_PORT=xxx
#FTP 服务器 用户名
FTP_SERVER_USERNAME=xxx
#FTP 服务器 用户密码
FTP_SERVER_PWD=xxxx
#FTP 服务器 默认目录
FTP_SERVER_DEFAULTDIR=/apache-tomcat-file/webapps/file/lhwy/upload
#FTP 服务器 文件访问路径前缀
FTP_SERVER_QUERY_PERFIX=http://xxx:xxx/file/lhwy/upload
2.导入commons-net-3.3.jar并且创建一个ftp工具类
public class FtpUtils {
private static FtpUtils instance = null;
private static FTPClient ftpClient = null;
private String hostName;
private int port;
private String userName;
private String password;
private FtpUtils(){}
public static FtpUtils getInstance() {
if (null == instance) {
instance = new FtpUtils();
}
ftpClient = new FTPClient();
return instance;
}
/**
* 初始化配置
* @param hostName 服务器地址
* @param port 端口号
* @param userName 用户名
* @param password 用户密码
*/
public void doInitConfig(String hostName,int port,String userName,String password) {
this.hostName = hostName;
this.port = port;
this.userName = userName;
this.password = password;
}
/**
* 连接
* @return 是否连接成功
*/
private boolean connect() {
if (ftpClient.isConnected()) {
return true;
}
try {
ftpClient.connect(hostName, port);
return true;
} catch (IO