From c46466b0ea030d2bcab50f1ec6aca8632c846634 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期二, 18 十月 2022 15:48:09 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/PathHelper.java | 83 ++++++++++++++++++++++++++++++++++++----- 1 files changed, 72 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/lf/server/helper/PathHelper.java b/src/main/java/com/lf/server/helper/PathHelper.java index be75b94..e0f964a 100644 --- a/src/main/java/com/lf/server/helper/PathHelper.java +++ b/src/main/java/com/lf/server/helper/PathHelper.java @@ -1,10 +1,14 @@ package com.lf.server.helper; import com.lf.server.config.PropertiesConfig; +import com.lf.server.entity.all.SettingData; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.File; +import java.util.Date; /** * 璺緞甯姪绫� @@ -15,8 +19,6 @@ @Autowired private PropertiesConfig config; - private static int MAX_FILES = 2000; - private static int importPath = 1; private static int exportPath = 1; @@ -24,6 +26,10 @@ private static int uploadPath = 1; private static int sharePath = 1; + + private final static double D92 = 92; + + private final static Log log = LogFactory.getLog(PathHelper.class); public PropertiesConfig getConfig() { return config; @@ -66,7 +72,7 @@ } File[] files = file.listFiles(); - if (files.length < MAX_FILES) { + if (files.length < SettingData.MAX_FILES) { return subPath; } @@ -76,8 +82,6 @@ /** * 鑾峰彇瀵煎叆鐩綍 - * - * @return */ public String getImportFullPath() { importPath = getSubPath(config.getImportPath(), importPath); @@ -87,8 +91,6 @@ /** * 鑾峰彇鍑哄浘鐩綍 - * - * @return */ public String getExportFullPath() { exportPath = getSubPath(config.getExportPath(), exportPath); @@ -98,8 +100,6 @@ /** * 鑾峰彇涓婁紶鐩綍 - * - * @return */ public String getUploadFullPath() { uploadPath = getSubPath(config.getUploadPath(), uploadPath); @@ -109,12 +109,73 @@ /** * 鑾峰彇鍏变韩鐩綍 - * - * @return */ public String getShareFullPath() { sharePath = getSubPath(config.getSharePath(), sharePath); return config.getSharePath() + File.separator + sharePath; } + + /** + * 鑾峰彇涓存椂璺緞 + */ + public String getTempPath() { + String tempName = FileHelper.getTempPath(); + String tempPath = config.getTempPath(); + String path = tempPath + File.separator + tempName; + + File file = new File(path); + if (!file.exists() && !file.isDirectory()) { + file.mkdirs(); + } + + double ran = Math.random() * 99; + if (ran > D92) { + deleteOldPath(tempPath); + } + + return path; + } + + /** + * 鍒犻櫎鏃ц矾寰� + */ + public void deleteOldPath(String tempPath) { + try { + File file = new File(tempPath); + String str = StringHelper.YMD__FORMAT.format(new Date()); + + File[] files = file.listFiles(); + for (File f : files) { + if (f.getPath().indexOf(str) > -1) { + continue; + } + + deleteFiles(f); + } + } catch (Exception ex) { + log.error(ex.getMessage() + ex.getStackTrace() + "\n"); + } + } + + /** + * 绾ц仈鍒犻櫎鏂囦欢 + */ + public void deleteFiles(File file) { + if (file == null || !file.exists()) { + return; + } + + if (file.isDirectory()) { + File[] files = file.listFiles(); + for (File f : files) { + if (f.isDirectory()) { + deleteFiles(f); + } else { + f.delete(); + } + } + } + file.delete(); + } } -- Gitblit v1.9.3