Explorar o código

feat:申诉导出

刘云鑫 hai 1 día
pai
achega
888923409c

+ 20 - 4
alien-entity/src/main/java/shop/alien/entity/store/excelVo/util/ExcelGenerator.java

@@ -3,22 +3,38 @@ package shop.alien.entity.store.excelVo.util;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.*;
 import java.lang.reflect.Field;
 import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.List;
 
 public class ExcelGenerator {
 
+    private static final Logger log = LoggerFactory.getLogger(ExcelGenerator.class);
+
+    /**
+     * 生成 Excel 到本地路径。会在写入前确保父目录存在(含 Linux 生产环境;旧实现仅在 Windows 下 mkdirs,
+     * 导致如 /usr/local/alien/file/excel/generate/ 未预先创建时出现 FileNotFoundException)。
+     */
     public static <T> String generateExcel(String filePath, List<T> dataList, Class<T> clazz) throws IOException {
         String osName = System.getProperty("os.name").toLowerCase();
         if (osName.contains("win")) {
             filePath = "c:/" + filePath;
-            String substring = filePath.substring(0, filePath.lastIndexOf("/"));
-            File dir = new File(substring);
-            if (!dir.exists()) {
-                dir.mkdirs();
+        }
+        Path outputPath = Paths.get(filePath);
+        Path parentDirectory = outputPath.getParent();
+        if (parentDirectory != null) {
+            try {
+                Files.createDirectories(parentDirectory);
+            } catch (IOException e) {
+                log.error("创建 Excel 输出目录失败, filePath={}, parentDirectory={}", filePath, parentDirectory, e);
+                throw e;
             }
         }
         // 创建一个新的工作簿