方式1:实现WebMvcConfigurer 的addResourceHandlers方法
package com.example.demo4; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @author admin * @version 1.0.0 * @ClassName WebMvcConfig.java * @Description TODO * @createTime 2021年05月26日 11:16:00 */ @Configuration public class WebMvcConfig implements WebMvcConfigurer { /** * @Author admin * @Description //TODO * @Date 2021/5/26 14:20 * @Param [registry] * @return void **/ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //将所有/images/** 访问都映射到classpath:/static/ 目录下或者classpath:/META-INF/resources/、classpath:/resources/、classpath:/public/ registry.addResourceHandler("/images/**") .addResourceLocations("classpath:/static/") //项目目录下的静态资源 .addResourceLocations("file:D:/Images/");//服务器本地磁盘上的静态资源 } }
方式2:在配置文件application.yml指定静态资源的访问路径
spring: mvc: static-path-pattern: /images/** web: resources: static-locations: file:D://Images