You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.8 KiB
112 lines
3.8 KiB
package com.zc.business.controller;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.IOException;
|
|
import java.time.Instant;
|
|
import java.time.temporal.ChronoUnit;
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
import org.apache.commons.net.ftp.FTPFile;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
|
|
@Component("FTPDeletion")
|
|
@Slf4j
|
|
public class FTPDeletion {
|
|
|
|
private static int retentionDays = 60; // 默认保存天数为60天
|
|
public void deleteEventFile() {
|
|
log.info("定时任务执行,当前保存天气设置天数:"+retentionDays+"当前时间:"+java.time.LocalTime.now());
|
|
new FTPDeletion().remoteFileDeletion();
|
|
}
|
|
//修改当前保存天数
|
|
public void deleteEventFile(int days) {
|
|
this.retentionDays = days;
|
|
}
|
|
|
|
//获取当前保存天数
|
|
public int getRetentionDays() {
|
|
return retentionDays;
|
|
}
|
|
// 递归方法来处理文件和目录的删除
|
|
private void deleteDirectoryRecursively(FTPClient ftpClient, String parentDirPath) throws IOException {
|
|
|
|
ftpClient.setControlEncoding("GBK");
|
|
|
|
FTPFile[] files = ftpClient.listFiles(parentDirPath);
|
|
//选择要保留的天数
|
|
Instant thirtyDaysAgo = Instant.now().minus(retentionDays, ChronoUnit.DAYS);
|
|
log.info("当前设置的文件保存天数为:"+FTPDeletion.retentionDays+"当前时间:"+java.time.LocalTime.now());
|
|
for (FTPFile file : files) {
|
|
String filePath = parentDirPath + "/" + file.getName();
|
|
|
|
if (file.isDirectory()) {
|
|
// 如果是目录,则递归调用
|
|
deleteDirectoryRecursively(ftpClient, filePath);
|
|
} else {
|
|
// 排除包含特定关键词的文件名
|
|
if (!file.getName().contains("事故")) {
|
|
// 删除30天前的文件
|
|
Instant lastModifiedTime = file.getTimestamp().toInstant();
|
|
if (lastModifiedTime.isBefore(thirtyDaysAgo)) {
|
|
boolean deleted = ftpClient.deleteFile(filePath);
|
|
if (deleted) {
|
|
log.info("已删除文件:"+filePath);
|
|
|
|
} else {
|
|
log.info("无法删除文件:"+filePath);
|
|
|
|
}
|
|
}
|
|
} else {
|
|
log.info("文件名包含关键词'事故',跳过删除: :"+file.getName());
|
|
}
|
|
}
|
|
}
|
|
|
|
// 检查并删除空目录(但不删除根目录)
|
|
if (!parentDirPath.equals("/")) {
|
|
FTPFile[] remainingFiles = ftpClient.listFiles(parentDirPath);
|
|
if (remainingFiles.length == 0) {
|
|
boolean removed = ftpClient.removeDirectory(parentDirPath);
|
|
if (removed) {
|
|
log.info("已删除目录:"+parentDirPath);
|
|
|
|
} else {
|
|
log.info("无法删除目录:"+parentDirPath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void remoteFileDeletion() {
|
|
String server = "192.168.3.1";
|
|
int port = 21;
|
|
String user = "1911390090@qq.com";
|
|
String password = "989878wxl";
|
|
|
|
try {
|
|
FTPClient ftpClient = new FTPClient();
|
|
ftpClient.setControlEncoding("GBK");
|
|
ftpClient.connect(server, port);
|
|
ftpClient.login(user, password);
|
|
ftpClient.enterLocalPassiveMode();
|
|
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
|
|
|
// 指定远程根目录
|
|
String remoteDirPath = "/";
|
|
// 从根目录开始递归删除
|
|
deleteDirectoryRecursively(ftpClient, remoteDirPath);
|
|
|
|
ftpClient.logout();
|
|
ftpClient.disconnect();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
}
|