springboot访问静态资源

一、默认资源映射
在Resources目录下新建/META-INF/resources、/resources、/static、/public四个中任意一个。

2016-01-08 09:29:30.362  INFO 24932 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-08 09:29:30.362  INFO 24932 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-08 09:29:30.437  INFO 24932 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

其中默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources)
其中默认配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/
如果按如下结构存放相同名称的图片,那么Spring Boot 读取图片的优先级是怎样的呢?



当我们访问地址 http://127.0.0.1:10002/fengjing.jpg 的时候,显示哪张图片?这里博主可以直接告诉大家,优先级顺序为:META/resources > resources > static > public
如果我们想访问pic2.jpg,请求地址 http://127.0.0.1:10002/img/pic2.jpg
二、自定义资源映射
上面我们介绍了Spring Boot 的默认资源映射,一般够用了,那我们如何自定义目录?
这些资源都是打包在jar包中的,然而实际应用中,我们还有很多资源是在管理系统中动态维护的,并不可能在程序包中,对于这种随意指定目录的资源,如何访问?
1、通过代码设置
① 自定义目录
以增加 /myres/* 映射到 classpath:/myres/* 为例的代码处理为:
实现类继承 WebMvcConfigurerAdapter 并重写方法 addResourceHandlers (对于 WebMvcConfigurerAdapter 上篇介绍拦截器的文章中已经有提到)
@Configuration
public class MyWebAppConfigurer 
        extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/myres/**").addResourceLocations("classpath:/myres/");
        super.addResourceHandlers(registry);
    }

}

访问templates 文件夹中的fengjing.jpg 图片的地址为 http://localhost:8080/myres/fengjing.jpg
这样使用代码的方式自定义目录映射,并不影响Spring Boot的默认映射,可以同时使用,因为两者的虚拟路径不同。
如果我们将/myres/* 修改为 /* 与默认的相同时,则会覆盖系统的配置,可以多次使用 addResourceLocations 添加目录,优先级先添加的高于后添加的。
// 访问templates根目录下的fengjing.jpg 的URL为 http://localhost:8080/fengjing.jpg (/** 会覆盖系统默认的配置)
// registry.addResourceHandler("/**").addResourceLocations("classpath:/templates/").addResourceLocations("classpath:/rules/");

其中 addResourceLocations 的参数是动参,可以这样写 addResourceLocations(“classpath:/img1/”, “classpath:/img2/”, “classpath:/img3/”);
重新编译后的路径为下图


② 使用外部目录
如果我们要指定一个绝对路径的文件夹(如 E:/myimgs/ ),则只需要使用 addResourceLocations 指定即可。访问E:/myimgs/文件夹中的fengjing.jpg 图片的地址为 http://localhost:8080/myimgs/fengjing.jpg
// 可以直接使用addResourceLocations 指定磁盘绝对路径,同样可以配置多个位置,注意路径写法需要加上file:
// addResourceHandler("/myimgs/**")中的myimgs对应url中的myimgs
// addResourceLocations("file:E:/myimgs/")中是图片的绝对路径
   registry.addResourceHandler("/myimgs/**").addResourceLocations("file:E:/myimgs/");

注:如果使用外部目录,那么maven编译之后的jar包里就可以不需要有静态资源,在服务器相应的目录下(与程序中自定义的目录一致)存放该静态资源,运行jar后照样可以获取到。
2、通过配置文件配置
上面是使用代码来定义静态资源的映射,其实Spring Boot也为我们提供了可以直接在 application.properties(或.yml)中配置的方法。
配置方法如下:
使用 spring.mvc.static-path-pattern 可以重新定义pattern,如修改为 /myres/** ,则访问static 等目录下的fengjing.jpg文件应该为 http://localhost:8080/myres/fengjing.jpg ,修改之前为 http://localhost:8080/fengjing.jpg
注意 spring.mvc.static-path-pattern 只可以定义一个,目前不支持多个逗号分割的方式。
# 默认值为 /**
spring.mvc.static-path-pattern=(默认值为注释的部分)

使用 spring.resources.static-locations 可以重新定义 pattern 所指向的路径,支持 classpath: 和 file: (上面已经做过说明)
# 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 
spring.resources.static-locations=(①默认值为注释的部分,多个使用英文逗号隔开;②指向的路径 file:E:/myimgs/)
原文地址:https://www.cnblogs.com/hzcya1995/p/13317311.html