From 4f3a98f19143865cdc1de4791e8a95d96bd40c65 Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期五, 01 八月 2025 13:27:59 +0800
Subject: [PATCH] yys 密码已重置

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/FileSaveUtil.java |  179 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 179 insertions(+), 0 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/FileSaveUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/FileSaveUtil.java
new file mode 100644
index 0000000..da4ca5f
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/FileSaveUtil.java
@@ -0,0 +1,179 @@
+package com.ruoyi.common.utils;
+
+import com.alibaba.excel.util.IoUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.util.FileSystemUtils;
+import org.springframework.util.ObjectUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.PostConstruct;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Random;
+
+/**
+ * 淇濆瓨鏂囦欢宸ュ叿
+ */
+@Slf4j
+@Component
+public class FileSaveUtil {
+
+    // 鍙杫ml涓殑璺緞 + /
+    private static String FILE_PATH;
+
+    private static String WORD_URL_PATH;
+
+    private static String[] ALLOWED;
+
+    @Value("${file.path}")
+    private String file;
+
+    @Value("${wordUrl}")
+    private String wordUrl;
+
+    @Value("${file.allowed}")
+    private String[] allowed;
+
+    @PostConstruct
+    public void getFile() {
+        FILE_PATH = this.file;
+    }
+
+    @PostConstruct
+    public void getWordUrl(){
+        WORD_URL_PATH = this.wordUrl;
+    }
+
+    @PostConstruct
+    public void getAllowed(){
+        ALLOWED = this.allowed;
+    }
+
+    /**
+     * 瀛樺偍鏂囦欢涓诲嚱鏁�
+     * @param file 鏂囦欢浜岃繘鍒舵祦
+     * @return 杩斿洖鏂囦欢鍚嶇О鐢ㄤ簬瀛樺偍鏁版嵁搴�
+     */
+    public static String StoreFile(MultipartFile file) {
+        String originalFilename = file.getOriginalFilename();
+        // 鐢熸垚闅忔満鍚嶇О锛氭椂闂確闅忔満6浣嶆暟瀛�
+        String FileName = System.currentTimeMillis() + "_" + getNumber(6);
+        String suffix = null;
+        if (originalFilename != null) {
+            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
+            // 濡傛灉鍚庣紑鍚嶄笉閫氳繃鎶涘嚭寮傚父
+            if (!isFileAllowed(suffix)){
+                throw new RuntimeException(suffix);
+            }
+        }
+        // 鍚嶇О鎷兼帴
+        String fileName = FileName + suffix;
+        // 杩涜瀛樺偍
+        try {
+            storeFileWithFileName(file.getBytes(), fileName);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        return fileName;
+    }
+
+    public static Boolean DeleteFile(String fileName) {
+        if (ObjectUtils.isEmpty(fileName)) {
+            return false;
+        }
+        return FileSystemUtils.deleteRecursively(new File(FILE_PATH + "/" + fileName));
+    }
+    /**
+     * 瀛樺偍鏂囦欢鍑芥暟
+     * @param content 鏂囦欢浜岃繘鍒舵祦
+     * @param fileName 鏂囦欢鍚嶇О
+     */
+    private static void storeFileWithFileName(byte[] content, String fileName) {
+        // 瀛樺偍璺緞
+        String path = FILE_PATH + File.separatorChar;
+        // 鐩綍涓嶅瓨鍦ㄥ垯鍒涘缓
+        File file = new File(path);
+        if (!file.exists()) {
+            file.mkdirs();
+        }
+        // 寮�濮嬪瓨鍌�
+        try (FileOutputStream os = new FileOutputStream(path + fileName);
+             ByteArrayInputStream is = new ByteArrayInputStream(content)) {
+             IoUtils.copy(is, os);
+        } catch (IOException e) {
+            throw new RuntimeException("鏂囦欢瀛樺偍鏍煎紡寮傚父");
+        }
+    }
+
+    /**
+     * 鍒ゆ柇鏂囦欢鏄惁琚厑璁镐笂浼�
+     *
+     * @param fileName 鏂囦欢鍚�
+     * @return 鍏佽true, 鍚﹀垯false
+     */
+    private static boolean isFileAllowed(String fileName) {
+        // 鑾峰彇鍚庣紑鍚�
+        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
+        for (String allow : ALLOWED) {
+            if (allow.equals(suffixName)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 淇濆瓨鏂囦欢鍒皐ord鏂囦欢澶归噷
+     * @param file
+     * @return
+     */
+    public static String uploadWordFile(MultipartFile file) {
+        String urlString;
+        String pathName;
+        String path;
+        try {
+            String contentType = file.getContentType();
+            if (contentType != null && contentType.startsWith("image/")) {
+                // 鏄浘鐗�
+                path = FILE_PATH;
+            } else {
+                // 鏄枃浠�
+                path = WORD_URL_PATH;
+            }
+            File realpath = new File(path);
+            if (!realpath.exists()) {
+                realpath.mkdirs();
+            }
+            pathName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmss")) + "-" + file.getOriginalFilename();
+            urlString = realpath + "/" + pathName;
+            file.transferTo(new File(urlString));
+            return pathName;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+
+
+    /**
+     * 鑾峰彇闅忔満鏁板瓧
+     * @param n 浣嶆暟
+     * @return 杩斿洖闅忔満鍊�
+     */
+    public static String getNumber(int n) {
+        char[] chars = "1234567890".toCharArray();
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < n; i++) {
+            char c = chars[new Random().nextInt(chars.length)];
+            sb.append(c);
+        }
+        return sb.toString();
+    }
+}

--
Gitblit v1.9.3