默认静态资源访问
Spring Boot的默认静态资源的路径为:
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
优先级从从高到低。
在SpringBoot中可以直接在配置文件中覆盖默认的静态资源路径的配置信息:
application.properties
配置文件如下:web.upload-path=E:/jsr_img/ spring.mvc.static-path-pattern=/PHOTO/** spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
注意:
①web.upload-path
这个属于自定义的属性,指定了一个路径,注意要以/
结尾;
②spring.mvc.static-path-pattern=/PHOTO/**
表示所有的访问都经过静态资源路径;
③spring.resources.static-locations
在这里配置静态资源路径,前面说了这里的配置是覆盖默认配置,所以需要将默认的也加上,否则static
、public
等这些路径将不能被当作静态资源路径,在这里的最末尾加上file:${web.upload-path}
。
之所以要加file:
是因为要在这里指定一个具体的硬盘路径,其他的使用classpath
指定的是系统环境变量;
访问图片:
http://localhost:端口号/数据库中的图片路径
其他配置文件:
spring.http.multipart.enabled=true #默认支持文件上传.
spring.http.multipart.file-size-threshold=0 #支持文件写入磁盘.
spring.http.multipart.location= # 上传文件的临时目录
spring.http.multipart.max-file-size=1Mb # 最大支持文件大小
spring.http.multipart.max-request-size=10Mb # 最大支持请求大小