From ac93c3a1bc1e759d4f40566fc2201ce7c480ee96 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期二, 18 十月 2022 12:02:50 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/PathHelper.java | 78 ++++++++++++++- src/main/java/com/lf/server/service/data/UploaderService.java | 121 ++++++++--------------- src/main/java/com/lf/server/entity/ctrl/FileInfo.java | 54 ++++++++++ 3 files changed, 166 insertions(+), 87 deletions(-) diff --git a/src/main/java/com/lf/server/entity/ctrl/FileInfo.java b/src/main/java/com/lf/server/entity/ctrl/FileInfo.java new file mode 100644 index 0000000..22a0687 --- /dev/null +++ b/src/main/java/com/lf/server/entity/ctrl/FileInfo.java @@ -0,0 +1,54 @@ +package com.lf.server.entity.ctrl; + +/** + * 鏂囦欢淇℃伅 + * @author WWW + */ +public class FileInfo { + private String fileName; + + private Long size; + + private String md5; + + private String path; + + public FileInfo() { + } + + public FileInfo(String fileName) { + this.fileName = fileName; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public Long getSize() { + return size; + } + + public void setSize(Long size) { + this.size = size; + } + + public String getMd5() { + return md5; + } + + public void setMd5(String md5) { + this.md5 = md5; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } +} diff --git a/src/main/java/com/lf/server/helper/PathHelper.java b/src/main/java/com/lf/server/helper/PathHelper.java index fe2eaca..e0f964a 100644 --- a/src/main/java/com/lf/server/helper/PathHelper.java +++ b/src/main/java/com/lf/server/helper/PathHelper.java @@ -2,10 +2,13 @@ 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; /** * 璺緞甯姪绫� @@ -23,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; @@ -75,8 +82,6 @@ /** * 鑾峰彇瀵煎叆鐩綍 - * - * @return */ public String getImportFullPath() { importPath = getSubPath(config.getImportPath(), importPath); @@ -86,8 +91,6 @@ /** * 鑾峰彇鍑哄浘鐩綍 - * - * @return */ public String getExportFullPath() { exportPath = getSubPath(config.getExportPath(), exportPath); @@ -97,8 +100,6 @@ /** * 鑾峰彇涓婁紶鐩綍 - * - * @return */ public String getUploadFullPath() { uploadPath = getSubPath(config.getUploadPath(), uploadPath); @@ -108,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(); + } } diff --git a/src/main/java/com/lf/server/service/data/UploaderService.java b/src/main/java/com/lf/server/service/data/UploaderService.java index a1f3bf7..c5cb523 100644 --- a/src/main/java/com/lf/server/service/data/UploaderService.java +++ b/src/main/java/com/lf/server/service/data/UploaderService.java @@ -5,6 +5,7 @@ import com.lf.server.entity.all.HttpStatus; import com.lf.server.entity.all.ResponseMsg; import com.lf.server.entity.all.SettingData; +import com.lf.server.entity.ctrl.FileInfo; import com.lf.server.entity.sys.AttachEntity; import com.lf.server.entity.sys.UserEntity; import com.lf.server.helper.FileHelper; @@ -31,9 +32,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.net.URLEncoder; -import java.util.Date; -import java.util.List; -import java.util.UUID; +import java.util.*; /** * 鏁版嵁涓婁紶鏈嶅姟绫� @@ -46,8 +45,6 @@ @Autowired AttachService attachService; - - private final static double D92 = 92; private final static long ONE_DAY = 24 * 60 * 60 * 1000; @@ -73,7 +70,7 @@ // 浼犺緭鏂囦欢 String oldName = file.getOriginalFilename(); - String filePath = pathHelper.getConfig().getTempPath() + File.separator + oldName; + String filePath = pathHelper.getTempPath() + File.separator + oldName; File newFile = new File(filePath); file.transferTo(newFile); @@ -207,64 +204,66 @@ // list瀹瑰櫒鐢ㄦ潵淇濆瓨琛ㄥ崟涓殑鎵�鏈夋暟鎹俊鎭� List<FileItem> items = sfu.parseRequest(ctx); - // 閬嶅巻瀹瑰櫒锛屽鐞嗚В鏋愮殑鍐呭锛氫竴涓鐞嗘櫘閫氳〃鍗曞煙锛屼竴涓鐞嗘枃浠剁殑琛ㄥ崟鍩� - for (FileItem item : items) { - if (item.isFormField()) { - handleFormField(item); - } else { - handleFileField(item, req); - } + copeFileItems(items, req); + } catch (Exception ex) { + log.error(ex.getMessage() + ex.getStackTrace() + "\n"); + } + } + + /** + * 澶勭悊瑙f瀽鍐呭锛氬鐞嗘櫘閫氳〃鍗曞煙鍜屾枃浠惰〃鍗曞煙 + */ + private void copeFileItems(List<FileItem> items, HttpServletRequest req) throws Exception { + Map<String, String> map = new HashMap<String, String>(3); + List<FileInfo> list = new ArrayList<FileInfo>(); + + String path = pathHelper.getTempPath(); + for (FileItem item : items) { + if (item.isFormField()) { + String key = item.getFieldName(); + String value = item.getString("utf-8"); + map.put(key, value); + continue; } - } catch (Exception ex) { - log.error(ex.getMessage() + ex.getStackTrace() + "\n"); + + FileInfo fi = copeFile(item, path, req); + if (fi != null) { + list.add(fi); + } } } /** - * 澶勭悊 鏅�氭暟鎹」 - * - * @param item + * 澶勭悊鏂囦欢 */ - private void handleFormField(FileItem item) { - // 鑾峰彇 鏅�氭暟鎹」涓殑 name鍊� - String fieldName = item.getFieldName(); - - // 鑾峰彇 鏅�氭暟鎹」涓殑 value鍊� - String value = ""; + private FileInfo copeFile(FileItem item, String path, HttpServletRequest req) { try { - // 浠� utf-8鐨勭紪鐮佹牸寮忔潵瑙f瀽 value鍊� - value = item.getString("utf-8"); + // 鑾峰彇鏂囦欢鍚嶏紝鍒ゆ柇鏄惁鍚堟硶 + FileInfo fi = new FileInfo(item.getName()); + if (StringHelper.isEmpty(fi.getFileName())) { + return null; + } + + fi.setSize(item.getSize()); + fi.setPath(path + File.separator + fi.getFileName()); + item.write(new File(fi.getPath())); + + return fi; } catch (Exception ex) { log.error(ex.getMessage() + ex.getStackTrace() + "\n"); + return null; } - - // 杈撳嚭鍒版帶鍒跺彴 - System.out.println("fieldName:" + fieldName + "--value:" + value); } - /** - * 澶勭悊 鏂囦欢鏁版嵁椤� - * - * @param item - */ private void handleFileField(FileItem item, HttpServletRequest req) { // 鑾峰彇 鏂囦欢鏁版嵁椤逛腑鐨� 鏂囦欢鍚� String fileName = item.getName(); - - // 鍒ゆ柇 姝ゆ枃浠剁殑鏂囦欢鍚嶆槸鍚﹀悎娉� - if (fileName == null || "".equals(fileName)) { - return; - } // 鎺у埗鍙兘涓婁紶鍥剧墖 String img = "image"; if (!item.getContentType().startsWith(img)) { return; } - - // 灏嗘枃浠朵俊鎭緭鍑哄埌鎺у埗鍙�:鏂囦欢鍚�+鏂囦欢澶у皬 - System.out.println("fileName:" + fileName); - System.out.println("fileSize:" + item.getSize()); // 鑾峰彇 褰撳墠椤圭洰涓嬬殑 /files 鐩綍鐨勭粷瀵逛綅缃� ServletContext ctx = req.getSession().getServletContext(); @@ -282,42 +281,6 @@ item.write(new File(file.toString(), UUID.randomUUID() + "_" + fileName)); } catch (Exception e) { e.printStackTrace(); - } - } - - - /** - * 鑾峰彇涓存椂璺緞 - */ - public String getTempPath() { - String tempName = FileHelper.getTempPath(); - String tempPath = pathHelper.getConfig().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(); - } - - return path; - } - - public void deleteOldPath() { - try { - Long time = System.currentTimeMillis(); - for (int i = 1, c = 30; i < c; i++) { - Date date = new Date(time - i * ONE_DAY); - String str = StringHelper.YMD__FORMAT.format(date); - - // - } - } catch (Exception ex) { - log.error(ex.getMessage() + ex.getStackTrace() + "\n"); } } } -- Gitblit v1.9.3