wuchao před 4 měsíci
rodič
revize
ee55c4e67e

+ 28 - 1
renren-admin/src/main/java/io/renren/common/config/MultipartConfig.java

@@ -1,17 +1,44 @@
 package io.renren.common.config;
 
+import javax.annotation.Resource;
 import javax.servlet.MultipartConfigElement;
 
+import io.renren.common.exception.RRException;
+import io.renren.modules.componet.upload.utils.FileUtils;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.boot.web.servlet.MultipartConfigFactory;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
 @Configuration
 public class MultipartConfig {
+
+    @Resource
+    private ServerProperties serverProperties;
+
     @Bean
     public MultipartConfigElement multipartConfigElement() {
+        String absolutePath = serverProperties.getTomcat().getBasedir().getAbsolutePath();
+        String contextPath = serverProperties.getServlet().getContextPath();
+        String location = "/wjxyMultipart/";
+        // 转换为Path对象
+        Path dirPath = Paths.get(absolutePath + "/work/Tomcat/localhost/" + contextPath + location);
+        try {
+            // 若目录不存在则创建(包括所有父目录);若已存在则直接返回
+            Path createdPath = Files.createDirectories(dirPath);
+            System.out.println("目录准备完成:" + createdPath.toAbsolutePath());
+        } catch (IOException e) {
+            System.err.println("目录创建失败:" + e.getMessage());
+        }
+
         MultipartConfigFactory factory = new MultipartConfigFactory();
-        factory.setLocation("/wjxyMultipart");
+        factory.setLocation(location);
 //        factory.setLocation("/");
         return factory.createMultipartConfig();
     }