第一种方式:注入一个WebMvcConfigurer对象(springboot中所有的WebMvcConfigurer对象会一起起作用)
import org.springframework.context.annotation.Bean;
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 MvcConfig {
@Bean
public WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/uploads/**").addResourceLocations("file:/home/uploads/");
}
};
}
“/uploads/**” :表示访问路径,根据实际情况指定(这里表示/uploads/下的所有路径)
“file:/home/uploads/” :表示静态资源在硬盘上的真实存储位置,根据实际情况指定
第二种方式:配置文件中配置(常用)
mvc:
static-path-pattern: /uploads/**
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:/home/uploads/
————————————————
版权声明:本文为CSDN博主「yangxinhu_coder」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42425970/article/details/90727297
只需要将static-locations下末尾的真实路径file:/home/upload/修改成实际需要映射到的真实路径即可