好得很程序员自学网

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

springboot文件虚拟路径映射方式

springboot文件虚拟路径映射

在application.properties配置文件中配置

?

1

2

3

spring.http.multipart.location= D:/

spring.mvc. static -path-pattern=/**

spring.resources. static -locations=  classpath:/,  file:${spring.http.multipart.location}

表示:读取文件时从我们的磁盘D中读取文件,例如:我们数据库中存取的文件路径为:

/2018/06/21//danPicture/cefb656f8cf542968107ca51e15c4ee5-1529540867019.png

那么在页面中展示的图片的路径实际上是我们本机电脑的路径:

D:/2018/06/21//danPicture/cefb656f8cf542968107ca51e15c4ee5-1529540867019.png

就是说会自动帮我们做一个拼接的操作!!!!

springboot 配置文件虚拟路径 供外部访问

配置虚拟路径 供外部访问图片 视频等文件

第一步:配置application.yml

?

1

2

3

4

5

spring:

mvc:

   static -path-pattern: /**

resources:

   static -locations: classpath\:/META-INF/resources/,classpath\:/resources/,classpath\:/ static /,classpath\:/ public /,file\:F:/wechatProject/upload/

其中 F:/wechatProject/upload/ 为文件绝对路径

第二步:添加 Configuration 文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration

public class MyWebAppConfiguration extends WebMvcConfigurerAdapter {

     @Override

     public void addResourceHandlers(ResourceHandlerRegistry registry) {

         /**

          * @Description: 对文件的路径进行配置, 创建一个虚拟路径/Path/** ,即只要在<img src="/Path/picName.jpg" />便可以直接引用图片

          *这是图片的物理路径  "file:/+本地图片的地址"

          * @Date: Create in 14:08 2017/12/20

          */

         registry.addResourceHandler( "/Path/**" ).addResourceLocations( "file:/F:/wechatProject/upload/" );

         super .addResourceHandlers(registry);

     }

}

重启

如果application.yml 没有配置

?

1

2

3

server:

   servlet:

     context-path: /sell

浏览器访问 http://localhost:8080/Path/11.html

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/qiuqiu1628480502/article/details/80758882

查看更多关于springboot文件虚拟路径映射方式的详细内容...

  阅读:31次