idea配置FTP
连接测试
打开
工具拦打开
maven依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
FTP-配置
@Data
@RefreshScope
@ConfigurationProperties(prefix = "distribution")
public class EquityProperties {
private String ftpIp;
private int ftpPort;
private String ftpPath;
private String ftpUsername;
private String ftpPassword;
}
application.yml 配置
distribution:
ftpIp: 127.0.0.1
ftpPort: 21
ftpPath: /xxxx
ftpUsername: ftptest
ftpPassword: ftptest
文件上传工具类
@Component
public class FTPClientUtil {
private static final Logger logger = LoggerFactory.getLogger(FTPClientUtil.class);
private static final FTPClient FTP_CLIENT = new FTPClient();
@Resource
private EquityProperties properties;
private void initClient(int fileType) throws IOException {
FTP_CLIENT.connect(properties.getFtpIp(), properties.getFtpPort());
FTP_CLIENT.login(properties.getFtpUsername(), properties.getFtpPassword());
int replyCode = FTP_CLIENT.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
logger.error("FTP服务器拒绝连接。响应代码: {}", replyCode);
return;
} else {
logger.info("已成功连接到FTP服务器.");
}
if (!FTP_CLIENT.login(properties.getFtpUsername(), properties.getFtpPassword())) {
logger.error("FTP登录失败.");
return;
}
FTP_CLIENT.enterLocalPassiveMode();
FTP_CLIENT.setFileType(fileType);
}
public boolean uploadFileContent(String remoteDir, String remoteFileName, String fileContent) {
try {
initClient(FTP.BINARY_FILE_TYPE);
String remoteDirPath = properties.getFtpPath() + "/" + remoteDir;
if (!remoteDirPath.isEmpty() && !FTP_CLIENT.changeWorkingDirectory(remoteDirPath)) {
String[] dirs = remoteDirPath.split("/");
String currentDir = "/";
for (String dir : dirs) {
if (dir.isEmpty()) continue;
currentDir += dir + "/";
if (!FTP_CLIENT.changeWorkingDirectory(currentDir)) {
if (!FTP_CLIENT.makeDirectory(currentDir)) {
logger.error("无法创建目录: {}", currentDir);
return false;
}
FTP_CLIENT.changeWorkingDirectory(currentDir);
}
}
}
if (!FTP_CLIENT.changeWorkingDirectory(remoteDirPath)) {
System.out.println("目录不存在或无法访问: " + remoteDirPath);
return false;
}
InputStream input = new ByteArrayInputStream(fileContent.getBytes());
boolean done = FTP_CLIENT.storeFile(remoteFileName, input);
input.close();
if (!done) {
logger.error("上传文件失败。服务器响应: {}", FTP_CLIENT.getReplyString());
} else {
logger.info("文件上传成功.");
}
return done;
} catch (IOException ex) {
logger.error("发生IO异常: {}", ex.getMessage());
return false;
} finally {
closeFtpClient();
}
}
public boolean downloadFile(String remoteFilePath, String localFilePath) {
try {
initClient(FTP.BINARY_FILE_TYPE);
try (OutputStream output = new FileOutputStream(localFilePath)) {
boolean success = FTP_CLIENT.retrieveFile(remoteFilePath, output);
if (success) {
logger.info("文件下载成功.");
} else {
logger.info("下载文件失败.");
}
return success;
}
} catch (IOException ex) {
logger.error("下载文件失败,失败原因:{}", ex.getMessage());
return false;
} finally {
closeFtpClient();
}
}
public boolean deleteFile(String remoteFilePath) {
try {
initClient(FTP.BINARY_FILE_TYPE);
boolean deleted = FTP_CLIENT.deleteFile(remoteFilePath);
if (deleted) {
logger.info("文件删除成功.");
} else {
logger.info("文件删除失败.");
}
return deleted;
} catch (IOException ex) {
logger.error("删除文件失败,失败原因:{}", ex.getMessage());
return false;
} finally {
closeFtpClient();
}
}
private void closeFtpClient() {
if (FTPClientUtil.FTP_CLIENT.isConnected()) {
try {
FTPClientUtil.FTP_CLIENT.logout();
FTPClientUtil.FTP_CLIENT.disconnect();
logger.info("FTP客户端已关闭");
} catch (IOException ex) {
logger.error("FTP客户端关闭失败,失败原因:{}", ex.getMessage());
}
}
}
}