这篇文章主要介绍了springboot文件虚拟路径映射方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
目录
springboot文件虚拟路径映射
在application.properties配置文件中配置
springboot 配置文件虚拟路径 供外部访问
第一步:配置application.yml
重启
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
D:/2018/06/21//danPicture/cefb656f8cf542968107ca51e15c4ee5-1529540867019.png
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。