package com.terra.system.helper; import com.terra.system.config.PropertiesConfig; import com.terra.system.entity.all.SettingData; import com.terra.common.entity.all.StaticData; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.annotation.Resource; import org.springframework.stereotype.Component; import java.io.File; import java.net.URL; import java.util.Date; /** * 路径帮助类 * @author WWW */ @Component public class PathHelper { @Resource private PropertiesConfig config; private static int downloadPath = 1; private static int uploadPath = 1; private final static Log log = LogFactory.getLog(PathHelper.class); public PropertiesConfig getConfig() { return config; } public static int getDownloadPath() { return downloadPath; } public static int getUploadPath() { return uploadPath; } /** * 初始化 */ public void init() { downloadPath = getSubPath(config.getDownloadPath(), downloadPath); uploadPath = getSubPath(config.getUploadPath(), uploadPath); } private static int getSubPath(String parentPath, int subPath) { while (true) { String path = parentPath + File.separator + subPath; File file = new File(path); if (!file.exists() && !file.isDirectory()) { file.mkdirs(); return subPath; } File[] files = file.listFiles(); if (null == files || files.length < SettingData.MAX_FILES) { return subPath; } subPath++; } } /** * 获取下载完整目录 */ public String getDownloadFullPath() { downloadPath = getSubPath(config.getDownloadPath(), downloadPath); return config.getDownloadPath() + File.separator + downloadPath; } /** * 获取上传完整目录 */ public String getUploadFullPath() { uploadPath = getSubPath(config.getUploadPath(), uploadPath); return config.getUploadPath() + File.separator + uploadPath; } /** * 获取临时路径 */ public String getTempPath() { String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date()); String path = config.getTempPath() + File.separator + tempName; File file = new File(path); if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } deleteOldPath(config.getTempPath()); return path; } /** * 获取临时路径 */ public String getTempPath(String subPath) { if (!StringHelper.isEmpty(subPath)) { String path = config.getTempPath() + File.separator + subPath; File file = new File(path); if (file.exists() && file.isDirectory()) { return path; } } deleteOldPath(config.getTempPath()); return getTempPath(); } /** * 获取临时路径名称 */ public String getTempPathName() { String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date()); String path = config.getTempPath() + File.separator + tempName; File file = new File(path); if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } deleteOldPath(config.getTempPath()); return tempName; } /** * 删除旧路径 */ public void deleteOldPath(String tempPath) { try { double ran = Math.random() * 99; if (ran < StaticData.D90) { return; } File file = new File(tempPath); String str = StringHelper.YMD2_FORMAT.format(new Date()); File[] files = file.listFiles(); if (null != files) { for (File f : files) { if (f.getPath().contains(str)) { continue; } FileHelper.deleteFiles(f); } } } catch (Exception ex) { log.error(ex.getMessage(), ex); } } /** * 获取static目录 */ public static String getStaticPath() { ClassLoader classLoader = PathHelper.class.getClassLoader(); URL url = classLoader.getResource(""); String path = url.getPath(); return path.substring(0, path.indexOf("/target/classes")) + "/src/main/resources/static/"; } }