feat:DelFileManager
This commit is contained in:
parent
27dea46773
commit
d05c303044
|
@ -23,4 +23,6 @@ public interface DateConstant {
|
|||
String TIME_END = " 23:59:59";
|
||||
|
||||
String TIME_ZONE = "GMT+08:00";
|
||||
|
||||
int DAY_SECONDS = 24 * 60 * 60;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,127 @@
|
|||
package org.jeecg.modules;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.jeecg.common.constant.DateConstant;
|
||||
import org.jeecg.common.util.SpringContextUtils;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
@Component
|
||||
public class DelFileManager{
|
||||
|
||||
public void start() {
|
||||
DelFileThread delFileThread = new DelFileThread();
|
||||
delFileThread.start();
|
||||
}
|
||||
|
||||
private class DelFileThread extends Thread{
|
||||
|
||||
private boolean m_runFlag;
|
||||
private long m_sleepTime;
|
||||
private long m_interTime;
|
||||
|
||||
private List<String> m_delteFileList;
|
||||
|
||||
private DelFileThread(){
|
||||
this.m_runFlag = true;
|
||||
this.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (m_runFlag) {
|
||||
for (String dirPath : m_delteFileList) {
|
||||
delDirFile(dirPath);
|
||||
delEmptyDir(dirPath);
|
||||
}
|
||||
try {
|
||||
sleep(m_sleepTime);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化方法
|
||||
*/
|
||||
private void init(){
|
||||
Environment env = SpringContextUtils.getBean(Environment.class);
|
||||
String rootPath = env.getProperty("filesystem.rootPath");
|
||||
String emlPath = rootPath + File.separator + env.getProperty("filesystem.emlPath");
|
||||
String logPath = rootPath + File.separator + env.getProperty("filesystem.logPath");
|
||||
m_delteFileList = new ArrayList<>();
|
||||
m_delteFileList.add(emlPath);
|
||||
m_delteFileList.add(logPath);
|
||||
|
||||
// 默认删除三天前的文件
|
||||
Integer interDay = env.getProperty("filesystem.interDay", Integer.class, 3);
|
||||
m_interTime = interDay * DateConstant.DAY_SECONDS;
|
||||
|
||||
// 指定线程休眠时间(ms)
|
||||
m_sleepTime = DateConstant.DAY_SECONDS * 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定目录下的指定文件
|
||||
*/
|
||||
private boolean delDirFile(String dirPath){
|
||||
if (StrUtil.isBlank(dirPath))
|
||||
return false;
|
||||
|
||||
File dir = new File(dirPath);
|
||||
if (!dir.exists() || !dir.isDirectory())
|
||||
return true;
|
||||
|
||||
// 删除当前目录下所有文件
|
||||
List<File> files = ListUtil.toList(dir.listFiles());
|
||||
if (CollUtil.isEmpty(files))
|
||||
return true;
|
||||
|
||||
long nowSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
|
||||
for (File file : files) {
|
||||
if (file.isFile()){
|
||||
long lastModified = file.lastModified();
|
||||
long modifiedSeconds = TimeUnit.MILLISECONDS.toSeconds(lastModified);
|
||||
long interTime = nowSeconds - modifiedSeconds;
|
||||
if(interTime >= m_interTime) {
|
||||
file.delete();
|
||||
}
|
||||
}else {
|
||||
delDirFile(file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归删除指定目录下的所有空目录,但不删除指定目录本身
|
||||
*/
|
||||
private void delEmptyDir(String dirPath){
|
||||
if (StrUtil.isBlank(dirPath)) return;
|
||||
|
||||
File dir = new File(dirPath);
|
||||
if (!dir.exists() || !dir.isDirectory()) return;
|
||||
|
||||
List<File> files = ListUtil.toList(dir.listFiles());
|
||||
if (CollUtil.isEmpty(files)) return;
|
||||
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
delEmptyDir(file.getAbsolutePath());
|
||||
if (CollUtil.isEmpty(ListUtil.toList(file.listFiles())))
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -56,15 +56,4 @@ public class SpectrumFileController {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String zipFilePath = "C:\\Users\\a\\Desktop\\2.zip"; // 替换为你的zip文件路径
|
||||
String destDir = "C:\\Users\\a\\Desktop\\1"; // 替换为你的解压目标目录路径
|
||||
try {
|
||||
unzip(new File(zipFilePath), destDir);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import org.jeecg.common.properties.TaskProperties;
|
|||
import org.jeecg.common.util.DateUtils;
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
import org.jeecg.modules.AutoProcessManager;
|
||||
import org.jeecg.modules.DelFileManager;
|
||||
import org.jeecg.modules.FileSourceHandleManager;
|
||||
import org.jeecg.modules.UndealHandleManager;
|
||||
import org.jeecg.modules.email.EmailReceivePolicy;
|
||||
|
@ -44,6 +45,7 @@ public class JeecgAutoProcessApplication extends SpringBootServletInitializer im
|
|||
private final AutoProcessManager autoProcessManager;
|
||||
private final UndealHandleManager undealHandleManager;
|
||||
private final FileSourceHandleManager fileSourceHandleManager;
|
||||
private final DelFileManager delFileManager;
|
||||
private final Object ftpOpierationLock = new Object();
|
||||
|
||||
@Override
|
||||
|
@ -82,6 +84,8 @@ public class JeecgAutoProcessApplication extends SpringBootServletInitializer im
|
|||
autoProcessManager.start(systemStartupTime);
|
||||
undealHandleManager.start();
|
||||
fileSourceHandleManager.start();
|
||||
// 删除过期的文件
|
||||
delFileManager.start();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user