Springboot 静态资源路径虚拟映射配置

前言

静态资源路径是系统可以直接通过URL访问的路径,下面所有的文件用户可以直接获取。

springmvc可以通过xml配置的方式配置,这里说一下springboot中如何配置。

springboot默认的静态资源路径都是在 classpath 中的,有 :

classpath:/META-INF/resources/、classpath:/resources/、classpath:/static/、classpath:/public/

那么现在如果有一个上传文件到服务器的功能,放在上诉的文件夹中,不仅数据与代码不能分离,发布程序时还需要将之前的文件全部拷贝的jar包中,造成jar包过大。如果不拷贝的话,数据就会丢失。所以通常的做法是将静态资源路径设置到磁盘的某一个目录下。

Springboot配置

在springboot中,可以配置静态资源路径,覆盖默认的静态资源路径配置,bootstrap.yml配置如下:

server:
  port: 9999

web:
  #自定义的路径,结尾的/千万别忘了......
  upload-path: D:/smh/

spring:
  mvc:
    #所有访问都经过静态资源路径
    static-path-pattern: /**
  resources:
    #所有静态资源路径,file:指一个具体的磁盘路径
    static-locations: file:${web.upload-path},classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

假如D:/smh/ 下,有一张test.png,通过 http://localhost:9999/test.png 即可访问啦。

 

代码:

  mvc:
    static-path-pattern: /**
  resources:    
    static-locations: classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/, file:${spring.http.multipart.location} 

 

启动代码: java -jar ***.jar --spring.http.multipart.location=""

 

posted @ 2021-08-12 17:50  KJXY  阅读(873)  评论(0编辑  收藏  举报