有两种方式
第一种在配置文件中
- upload-path: /filename1/upload/
- spring:
- mvc:
- static-path-pattern: /static/**
- resources:
- static-locations: classpath:/METAINF/resources/,classpath:/resources/,classpath:/static/,classpath:/templates/,classpath:/public/,file:${upload-path}
第二种在java 代码
- package com.hwh.communitymanage.config;
-
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.CorsRegistry;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
- import java.io.File;
-
- @Configuration
- public class WebMvcConfig implements WebMvcConfigurer {
-
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- String uploadPath = "file:D:/upload-dir/";
- registry.addResourceHandler("/static/**").addResourceLocations(uploadPath );
- }
-
- }
注意:映射的路径static-path-pattern尽量不要设置为/**,设置为/**可能会出现找不到静态资源的情况