springboot实现通过路径从磁盘直接读取图片
作者:零落尘土 时间:2023-09-01 03:01:01
通过路径从磁盘直接读取图片
这段时间在做Springboot和Vue的例子,读取图片给出路径直接可以读,太方便了,一直么有搞懂为什么。
后面看到原来是在配置文件MyWebConfigurer中addResourceHandlers方法中增加了
registry.addResourceHandler("/api/file/**").addResourceLocations("file:" + "d:/workspace/img/");
@SpringBootConfiguration
public class MyWebConfigurer implements WebMvcConfigurer {
// * 省略
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/api/file/**").addResourceLocations("file:" + "d:/workspace/img/");
}
}
所有图片中的只要路径为http://localhost:9009/api/file/ule3di.jpg就会直接到文件夹下读取,不用另外多写代码,真的是太方便了。
需要主要的是,在图片保存的时候,需要保证路径保存正确即可。
访问本地(磁盘)图片
@Configuration
public class SpringMvcConfigurationInitializer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//上传的图片在c盘下的/opt/plate目录下,访问路径如
下:http://localhost:8088/opt/plate/icon_yxgl@2x.png
//其中plate表示访问的前缀。"file:/opt/plate/"是文件真实的存储路径
registry.addResourceHandler("/plate/**").addResourceLocations("file:/opt/plate/");
//file:/opt/plate/指向本地图片路径地址
super.addResourceHandlers(registry);
}
当然路径也可以是D盘、E盘,路径是可以指定的,如"file:D:/…“或"file:C:/…”
来源:https://blog.csdn.net/lijinqing39/article/details/99452448
标签:springboot,路径,磁盘,读取图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详谈Springfox与swagger的整合使用
2021-11-25 21:08:34
![](https://img.aspxhome.com/file/2023/5/77405_0s.png)
详解怎么用Java的super关键字
2021-10-06 08:11:14
![](https://img.aspxhome.com/file/2023/7/86807_0s.png)
Java 使用IO流实现大文件的分割与合并实例详解
2023-08-23 09:33:33
![](https://img.aspxhome.com/file/2023/8/58078_0s.png)
三道java新手入门面试题,通往自由的道路--锁+Volatile
2023-09-04 20:33:42
![](https://img.aspxhome.com/file/2023/7/68717_0s.jpg)
Java实现的断点续传功能的示例代码
2023-05-25 12:17:22
Android实现MVVM架构数据刷新详解流程
2023-07-05 13:33:41
![](https://img.aspxhome.com/file/2023/5/92615_0s.png)
利用Java代码写一个并行调用模板
2023-07-04 16:00:39
![](https://img.aspxhome.com/file/2023/0/95960_0s.webp)
mybatis关系映射之一对多和多对一
2021-08-01 21:14:26
![](https://img.aspxhome.com/file/2023/3/67973_0s.png)
Spring Boot中如何使用断路器详解
2022-03-03 06:34:49
![](https://img.aspxhome.com/file/2023/6/93066_0s.gif)
C#取得随机颜色的方法
2021-11-29 08:06:56
RecyclerView实现横向滚动效果
2023-11-24 02:26:30
一文带你搞懂Java中方法重写与方法重载的区别
2022-05-14 03:19:13
![](https://img.aspxhome.com/file/2023/5/101805_0s.webp)
Android短信接收监听、自动回复短信操作例子
2022-12-04 20:07:50
Android使用系统自带的相机实现一键拍照功能
2023-06-05 10:27:23
![](https://img.aspxhome.com/file/2023/0/137840_0s.png)
spring循环依赖策略解析
2022-09-13 13:49:10
![](https://img.aspxhome.com/file/2023/4/72304_0s.jpg)
java 动态生成SQL的实例讲解
2021-05-25 18:26:06
浅谈Android View滑动冲突的解决方法
2021-12-17 06:47:09
Java的线程与进程以及线程的四种创建方式
2022-12-01 14:11:07
![](https://img.aspxhome.com/file/2023/5/79905_0s.png)
Android 二维码 生成和识别二维码 附源码下载
2023-05-01 12:45:01
![](https://img.aspxhome.com/file/2023/8/87298_0s.jpg)
C#画笔Pen绘制自定义线的帽子
2022-01-09 20:33:55