Browse Source

文件上传图片获取优化

Linyt 1 year ago
parent
commit
c136773dfd

+ 23 - 0
java/src/main/java/boot/common/config/WebConfig.java

@@ -0,0 +1,23 @@
+package boot.common.config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class WebConfig implements WebMvcConfigurer {
+    @Value("${file.avatar}")
+    String avatar;
+    @Value("${file.path}")
+    String path;
+
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry) {
+        String avatarUtl = "file:" + avatar.replace("\\", "/");
+        String pathUtl = "file:" + path.replace("\\", "/");
+        registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
+        registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
+        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
+    }
+}

+ 5 - 5
java/src/main/resources/application.yml

@@ -13,9 +13,9 @@ jwt:
 single:
   login: false
 
-# 图片或文件存放的地方
+# 图片或文件存放的地方 ( 选择你想存放的地方!!! )
 file:
-  #头像存放的位置
-  avatar : F:\File\学习\实训\upload\avatar
-  #图片或文件存放的位置
-  path : F:\File\学习\实训\upload\file
+  #头像存放的位置 格式 F:\File\学习\实训\upload\avatar\
+  avatar : F:\File\学习\实训\upload\avatar\
+  #图片或文件存放的位置 格式 F:\File\学习\实训\upload\file\
+  path : F:\File\学习\实训\upload\file\

+ 3 - 3
java/target/classes/application.yml

@@ -13,9 +13,9 @@ jwt:
 single:
   login: false
 
-# 图片或文件存放的地方
+# 图片或文件存放的地方 ( 选择你想存放的地方!!! )
 file:
   #头像存放的位置
-  avatar : F:\File\学习\实训\upload\avatar
+  avatar : F:\File\学习\实训\upload\avatar\
   #图片或文件存放的位置
-  path : F:\File\学习\实训\upload\file
+  path : F:\File\学习\实训\upload\file\

BIN
java/target/classes/boot/common/config/WebConfig.class