From d05c3030443794fdeb79a48169dd6f254e272eb4 Mon Sep 17 00:00:00 2001 From: nieziyan Date: Wed, 25 Oct 2023 17:18:14 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9ADelFileManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jeecg/common/constant/DateConstant.java | 2 + .../org/jeecg/modules/DelFileManager.java | 127 ++++++++++++++++++ .../controller/SpectrumFileController.java | 11 -- .../jeecg/JeecgAutoProcessApplication.java | 4 + 4 files changed, 133 insertions(+), 11 deletions(-) create mode 100644 jeecg-module-auto-process/src/main/java/org/jeecg/modules/DelFileManager.java diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/DateConstant.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/DateConstant.java index 60891dfd..950a5b5d 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/DateConstant.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/DateConstant.java @@ -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; } diff --git a/jeecg-module-auto-process/src/main/java/org/jeecg/modules/DelFileManager.java b/jeecg-module-auto-process/src/main/java/org/jeecg/modules/DelFileManager.java new file mode 100644 index 00000000..84b5a851 --- /dev/null +++ b/jeecg-module-auto-process/src/main/java/org/jeecg/modules/DelFileManager.java @@ -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 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 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 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(); + } + } + } + } +} diff --git a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/controller/SpectrumFileController.java b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/controller/SpectrumFileController.java index ccc2b6ae..771f2f85 100644 --- a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/controller/SpectrumFileController.java +++ b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/controller/SpectrumFileController.java @@ -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(); - } - } - } diff --git a/jeecg-server-cloud/armd-auto-process-start/src/main/java/org/jeecg/JeecgAutoProcessApplication.java b/jeecg-server-cloud/armd-auto-process-start/src/main/java/org/jeecg/JeecgAutoProcessApplication.java index 40c28ed3..f8406508 100644 --- a/jeecg-server-cloud/armd-auto-process-start/src/main/java/org/jeecg/JeecgAutoProcessApplication.java +++ b/jeecg-server-cloud/armd-auto-process-start/src/main/java/org/jeecg/JeecgAutoProcessApplication.java @@ -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(); } /**