springboot文件虚拟路径映射方式

这篇文章主要介绍了springboot文件虚拟路径映射方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboot文件虚拟路径映射

在application.properties配置文件中配置

 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-600

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

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

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

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

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

第一步:配置application.yml

 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 文件

 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/** ,即只要在便可以直接引用图片 *这是图片的物理路径  "file:/+本地图片的地址" * @Date: Create in 14:08 2017/12/20 */ registry.addResourceHandler("/Path/**").addResourceLocations("file:/F:/wechatProject/upload/"); super.addResourceHandlers(registry); } } 

重启

如果application.yml 没有配置

 server: servlet: context-path: /sell

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

以上就是springboot文件虚拟路径映射方式的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java