好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

这段时间在做Springboot和Vue的例子,读取图片给出路径直接可以读,太方便了,一直么有搞懂为什么。

后面看到原来是在配置文件MyWebConfigurer中addResourceHandlers方法中增加了

?

1

registry.addResourceHandler( "/api/file/**" ).addResourceLocations( "file:" + "d:/workspace/img/" );

?

1

2

3

4

5

6

7

8

9

10

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

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

访问本地(磁盘)图片

?

1

2

3

4

5

6

7

8

9

10

11

12

@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实现通过路径从磁盘直接读取图片的详细内容...

  阅读:21次