软件编程
位置:首页>> 软件编程>> java编程>> springboot实现通过路径从磁盘直接读取图片

springboot实现通过路径从磁盘直接读取图片

作者:零落尘土  发布时间:2023-09-01 03:01:01 

标签:springboot,路径,磁盘,读取图片

通过路径从磁盘直接读取图片

这段时间在做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就会直接到文件夹下读取,不用另外多写代码,真的是太方便了。

需要主要的是,在图片保存的时候,需要保证路径保存正确即可。

springboot实现通过路径从磁盘直接读取图片

访问本地(磁盘)图片

@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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com