springboot访问静态资源

雨点打透心脏的1/2处 2021-12-03 14:19 642阅读 0赞
  1. 一、默认资源映射
  2. Resources目录下新建/META-INF/resources、/resources、/static、/public四个中任意一个。
  3. 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]
  4. 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]
  5. 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]
  6. 其中默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources)
  7. 其中默认配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/
  8. 如果按如下结构存放相同名称的图片,那么Spring Boot 读取图片的优先级是怎样的呢?
  9. 当我们访问地址 http://127.0.0.1:10002/fengjing.jpg 的时候,显示哪张图片?这里博主可以直接告诉大家,优先级顺序为:META/resources > resources > static > public
  10. 如果我们想访问pic2.jpg,请求地址 http://127.0.0.1:10002/img/pic2.jpg
  11. 二、自定义资源映射
  12. 上面我们介绍了Spring Boot 的默认资源映射,一般够用了,那我们如何自定义目录?
  13. 这些资源都是打包在jar包中的,然而实际应用中,我们还有很多资源是在管理系统中动态维护的,并不可能在程序包中,对于这种随意指定目录的资源,如何访问?
  14. 1、通过代码设置
  15. ① 自定义目录
  16. 以增加 /myres/* 映射到 classpath:/myres/* 为例的代码处理为:
  17. 实现类继承 WebMvcConfigurerAdapter 并重写方法 addResourceHandlers (对于 WebMvcConfigurerAdapter 上篇介绍拦截器的文章中已经有提到)
  18. @Configuration
  19. public class MyWebAppConfigurer
  20. extends WebMvcConfigurerAdapter {
  21. @Override
  22. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  23. registry.addResourceHandler("/myres/**").addResourceLocations("classpath:/myres/");
  24. super.addResourceHandlers(registry);
  25. }
  26. }
  27. 访问templates 文件夹中的fengjing.jpg 图片的地址为 http://localhost:8080/myres/fengjing.jpg
  28. 这样使用代码的方式自定义目录映射,并不影响Spring Boot的默认映射,可以同时使用,因为两者的虚拟路径不同。
  29. 如果我们将/myres/* 修改为 /* 与默认的相同时,则会覆盖系统的配置,可以多次使用 addResourceLocations 添加目录,优先级先添加的高于后添加的。
  30. // 访问templates根目录下的fengjing.jpg 的URL为 http://localhost:8080/fengjing.jpg (/** 会覆盖系统默认的配置)
  31. // registry.addResourceHandler("/**").addResourceLocations("classpath:/templates/").addResourceLocations("classpath:/rules/");
  32. 其中 addResourceLocations 的参数是动参,可以这样写 addResourceLocations(“classpath:/img1/”, “classpath:/img2/”, “classpath:/img3/”);
  33. 重新编译后的路径为下图
  34. ② 使用外部目录
  35. 如果我们要指定一个绝对路径的文件夹(如 E:/myimgs/ ),则只需要使用 addResourceLocations 指定即可。访问E:/myimgs/文件夹中的fengjing.jpg 图片的地址为 http://localhost:8080/myimgs/fengjing.jpg
  36. // 可以直接使用addResourceLocations 指定磁盘绝对路径,同样可以配置多个位置,注意路径写法需要加上file:
  37. // addResourceHandler("/myimgs/**")中的myimgs对应url中的myimgs
  38. // addResourceLocations("file:E:/myimgs/")中是图片的绝对路径
  39. registry.addResourceHandler("/myimgs/**").addResourceLocations("file:E:/myimgs/");
  40. 注:如果使用外部目录,那么maven编译之后的jar包里就可以不需要有静态资源,在服务器相应的目录下(与程序中自定义的目录一致)存放该静态资源,运行jar后照样可以获取到。
  41. 2、通过配置文件配置
  42. 上面是使用代码来定义静态资源的映射,其实Spring Boot也为我们提供了可以直接在 application.properties(或.yml)中配置的方法。
  43. 配置方法如下:
  44. 使用 spring.mvc.static-path-pattern 可以重新定义pattern,如修改为 /myres/** ,则访问static 等目录下的fengjing.jpg文件应该为 http://localhost:8080/myres/fengjing.jpg ,修改之前为 http://localhost:8080/fengjing.jpg
  45. 注意 spring.mvc.static-path-pattern 只可以定义一个,目前不支持多个逗号分割的方式。
  46. # 默认值为 /**
  47. spring.mvc.static-path-pattern=(默认值为注释的部分)
  48. 使用 spring.resources.static-locations 可以重新定义 pattern 所指向的路径,支持 classpath: 和 file: (上面已经做过说明)
  49. # 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
  50. spring.resources.static-locations=(①默认值为注释的部分,多个使用英文逗号隔开;②指向的路径 file:E:/myimgs/)

发表评论

表情:
评论列表 (有 0 条评论,642人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot 直接访问静态资源

    一般现在都前后端分离方式,SpringBoot主要提供接口服务,但有时候有一些小项目就希望一个jar前后端都搞定,因此一些页面等静态资源都放入SpringBoot中。 这里记录