<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>1.4.1</version>
</dependency>
package com.ifreegroup.releaseserver.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.stereotype.Component;
import java.io.*;
/**
* FTP上传下载工具类
*
* @author zy
* @date 2020-07-13 15:35
*/
@Slf4j
@Component
public class FtpUtil {
private FtpUtil() {
}
/**
* FTP客户端上传文件
*
* @param ip FTP地址
* @param port FTP端口
* @param userName FTP用户名
* @param password FTP用户名密码
* @param ftpPath FTP文件上传路径
* @param ftpName FTP上传文件名称
* @param loadPath 本地文件路径
* @return FTP是否上传成功
*/
public static boolean upload(String ip, int port, String userName, String password, String ftpPath, String
ftpName, String loadPath) {
boolean result = false;
FTPClient ftpClient = new FTPClient();
//设置被动模式
ftpClient.setRemoteVerificationEnabled(false);
if (login(ip, port, userName, password, ftpClient)) {
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if (ftpClient.changeWorkingDirectory(ftpPath)) {
log.info("==========文件目录:{}存在==========", ftpPath);
} else {
log.info("==========文件目录:{}不存在==========", ftpPath);
if (ftpClient.makeDirectory(ftpPath)) {
log.info("==========文件目录:{}创建成功=========", ftpPath);
} else {
log.info("==========需要创建多级目录==========");
createDirectory(ftpPath, ftpClient);
}
}
ftpClient.changeWorkingDirectory(ftpPath);
InputStream in = new FileInputStream(new File(loadPath));
if (ftpClient.storeFile(ftpName, in)) {
result = true;
log.info("==========FTP上传文件成功==========");
disconnect(ftpClient);
} else {
log.error("==========FTP上传文件失败==========");
}
in.close();
} catch (IOException e) {
log.error("==========异常信息{}==========", e);
e.printStackTrace();
}
}
return result;
}
/**
* FTP客户端下载文件
*
* @param ip FTP地址
* @param port FTP端口
* @param userName FTP用户名
* @param password FTP用户名密码
* @param ftpPath FTP文件上传路径
* @param loadPath 本地文件路径
* @param fileName 需要下载的文件名称
* @return FTP是否下载成功
*/
public static boolean download(String ip, int port, String userName, String password, String ftpPath, String
loadPath, String fileName) {
boolean result = false;
FTPClient ftpClient = new FTPClient();
//设置FTP客户端被动模式
ftpClient.setRemoteVerificationEnabled(false);
if (login(ip, port, userName, password, ftpClient)) {
try {
ftpClient.changeWorkingDirectory(ftpPath);
FTPFile[] fs = ftpClient.listFiles();
log.info("==========ftp文件:{}==========", fs);
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
// 根据绝对路径初始化文件
File localFile = new File(loadPath);
// 输出流
OutputStream is = new FileOutputStream(localFile);
// 下载文件
boolean ftpResult = ftpClient.retrieveFile(ff.getName(), is);
is.close();
if (ftpResult) {
log.info("==========FTP文件下载成功==========");
} else {
log.error("==========File文件转输出流失败==========");
}
}
}
result = true;
} catch (FileNotFoundException e) {
log.error("==========File文件转输出流失败==========");
e.printStackTrace();
} catch (IOException e) {
log.error("==========FTP文件输出失败==========");
e.printStackTrace();
}
}
return result;
}
/**
* FTP客户端连接
*
* @param ip FTP地址
* @param port FTP端口
* @param userName FTP用户名
* @param password FTP用户密码
* @return FTP是否连接成功
*/
private static boolean login(String ip, int port, String userName, String password, FTPClient ftpClient) {
boolean result = false;
//FTP连接
try {
//连接配置不可更改,更改容易导致文件上传下载读写部分出错
ftpClient.connect(ip, port);
//设置被动模式:必須设置在ftpClient登录之后
ftpClient.enterLocalPassiveMode();
//设置二进制文件格式
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//设置编码格式
ftpClient.setControlEncoding("UTF-8");
if (ftpClient.login(userName, password)) {
log.info("==========FTP ip:{},port:{},userName:{}登陆成功==========", ip, port, userName);
result = true;
} else {
log.error("==========FTP ip:{},port:{},userName:{}登陆失败==========", ip, port, userName);
disconnect(ftpClient);
}
} catch (IOException e) {
log.error("==========FTP ip:{},port:{}连接失败,请检查ip和端口是否正确==========", ip, port);
e.printStackTrace();
}
return result;
}
/**
* FTP客户端关闭
*
* @param ftpClient FTP客户端
*/
private static void disconnect(FTPClient ftpClient) {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
log.info("==========FTP客户端关闭成功==========");
} catch (IOException e) {
log.error("==========FTP客户端关闭失败==========");
e.printStackTrace();
}
}
}
/**
* FTP创建多级目录
*
* @param path 多级目录
* @param ftpClient FTP客户端
* @return 是否创建成功
*/
private static boolean createDirectory(String path, FTPClient ftpClient) {
boolean result = false;
String directory = path.substring(0, path.lastIndexOf("/") + 1);
try {
//判断是否为目录格式以及目录是否存在
if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(new String(directory.getBytes
("GBK"), "iso-8859-1"))) {
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
while (true) {
String subDirectory = new String(path.substring(start, end).getBytes("GBK"), "iso-8859-1");
if (!ftpClient.changeWorkingDirectory(subDirectory)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
} else {
log.error("==========FTP创建目录失败==========");
}
}
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
log.info("==========FTP创建目录成功==========");
result = true;
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}