前言
最近有个简单的项目需要通过网络地址访问本地照片,主打简单,所以不想用minio这些文件存储了,所以就研究了下用springboot直接访问本地照片
方案一
修改application.yml文件
1 2 3 4 5 6 7 8 9 10
| spring: mvc: static-path-pattern: /image/** resources: static-locations: file:E:/picture/
|
上面配置的意思是指,当我们访问项目的image路径时,就会映射到本地E盘的picture目录下。
假设在E盘的picture目录下有张test.png图片(这个图片可能是你的业务上传后保存的),那么我们通过
http://127.0.0.1:8080/image/test.png 就可以访问到这张图片。
上述方法有个弊端,在某些场景下,可能会无效,比如你的项目中写了某些过滤器等原因。另外,使用yml配置,也会使你静态访问路径失效,要注意。
方案二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
@Configuration public class WebConf extends WebMvcConfigurationSupport {
@Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/"); registry.addResourceHandler("/image/**").addResourceLocations("file:E:/picture/"); }
}
|
一如果不好使的时候可以使用二,适合简单改造使用,如果从长远考虑,最好还是使用minio这种文件存储比较好