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.
156 lines
5.7 KiB
156 lines
5.7 KiB
package com.zc.business.controller;
|
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
import com.ruoyi.system.service.ISysConfigService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.IOException;
|
|
import java.time.Instant;
|
|
import java.time.LocalDate;
|
|
import java.time.ZoneOffset;
|
|
import java.time.temporal.ChronoUnit;
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
import org.apache.commons.net.ftp.FTPFile;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
|
|
@Component()
|
|
@Slf4j
|
|
public class FTPDeletion {
|
|
ISysConfigService configService = SpringUtils.getBean(ISysConfigService.class);
|
|
|
|
/*
|
|
private static int retentionDays = 90; // 默认保存天数为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 {
|
|
|
|
int retentionDays= Integer.parseInt(configService.selectConfigByKey("EVENT-TIME"));//密钥
|
|
|
|
|
|
ftpClient.setControlEncoding("GBK");
|
|
|
|
FTPFile[] files = ftpClient.listFiles(parentDirPath);
|
|
//选择要保留的天数
|
|
Instant thirtyDaysAgo = Instant.now().minus(retentionDays, ChronoUnit.DAYS);
|
|
|
|
// 转换为本地日期
|
|
LocalDate localDate = thirtyDaysAgo.atZone(ZoneOffset.UTC).toLocalDate();
|
|
|
|
// 获取 30 天前当天的零点时间
|
|
Instant thirtyDaysAgoStartOfDay = localDate.atStartOfDay(ZoneOffset.UTC).toInstant();
|
|
log.info("当前设置的文件保存天数为:"+retentionDays+"当前时间:"+java.time.LocalTime.now());
|
|
for (FTPFile file : files) {
|
|
String filePath = parentDirPath + "/" + file.getName();
|
|
|
|
if (file.isDirectory()) {
|
|
System.out.println("地址"+filePath);
|
|
// 如果是目录,则递归调用
|
|
// 删除30天前的文件
|
|
Instant lastModifiedTime = file.getTimestamp().toInstant();
|
|
String[] split = filePath.split("交通事件");
|
|
if (split.length>1){
|
|
if (!lastModifiedTime.isBefore(thirtyDaysAgoStartOfDay)){
|
|
System.out.println("跳回天====="+filePath);
|
|
continue;
|
|
}
|
|
}
|
|
if (filePath.contains("ATTACHFILE")){
|
|
continue;
|
|
}
|
|
deleteDirectoryRecursively(ftpClient, filePath);
|
|
|
|
} else {
|
|
// 排除包含特定关键词的文件名
|
|
if (!file.getName().contains("事故")) {
|
|
// 删除30天前的文件
|
|
Instant lastModifiedTime = file.getTimestamp().toInstant();
|
|
if (lastModifiedTime.isBefore(thirtyDaysAgoStartOfDay)) {
|
|
boolean deleted = ftpClient.deleteFile(filePath);
|
|
if (deleted) {
|
|
log.info("已删除文件:"+filePath);
|
|
|
|
} else {
|
|
log.info("无法删除文件:"+filePath);
|
|
|
|
}
|
|
}else {
|
|
System.out.println("跳出循环-------------------------------------");
|
|
continue;
|
|
|
|
}
|
|
} 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() {
|
|
//10.0.111.12
|
|
String server= configService.selectConfigByKey("FTP-IP");//密钥
|
|
//String server = "192.168.3.1";
|
|
|
|
// int port = 21;
|
|
int port= Integer.parseInt(configService.selectConfigByKey("FTP-PORT"));
|
|
|
|
//ftpuser
|
|
//String user = "1911390090@qq.com";
|
|
String user= configService.selectConfigByKey("FTP-USER");//密钥
|
|
|
|
//Dxc123!@#
|
|
String password= configService.selectConfigByKey("FPT-PASSWORD");//密钥
|
|
//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();
|
|
}
|
|
}
|
|
|
|
|
|
}
|