public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/image/**").addResourceLocations("file:///D:/img/");
registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:favicon.ico");
super.addResourceHandlers(registry);
file:///D:/img/ 为D:/img下的全部 例如D:/img/index 显示为/image/index
file:///D:/img 为D:/img下的内容 这种方式读取不到 D:/img/index
linux 未测试
@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHand...
原文url:https://blog.csdn.net/ajklaclk/article/details/80804137
https://www.jianshu.com/p/54e0ca450f88
https://blog.csdn.net/ljj_9/article/details/79650008
https://blog.csdn.net/weixin_40050532/artic...
@Configuration
public class My
Web
MvcConfigurerAdapter extends
Web
MvcConfigurerAdapter {
//
获取
项目
根
路径
final File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
这个再Windows下是可以的,但是在Linux (centos7)下是不行的,正确的
获取
方法:
Resource resource = new ClassPathResource("");
System.out.println(resource.getFile().getAbsolutePath());
踩坑由此开始,先开始是用了查询各种绝对
路径
相对
路径
各种调试都试了,踩坑至半个多小时,也把对应的博文统统看了一遍,访问本地的资源要么在yml配置绝对
路径
,要么在addResourceLocations加你本地电脑的
路径
地址+ //即可,后续我图片地址可以访问了,但是音频访问不了,导致抓狂!
解决方案:
将音频的
路径
改了又改,也在...
开始我是这样设置的
registry.addResourceHandler("/upload/**").addResourceLocations("file:" + path); 完整代码如下图:
path对应的
路径
在 yml 文件中设置,如下图
然后
项目
启动后,通过在浏览器中直接访问文件的地址(例如:http://localhost:8090/app...
在看一个博客源码发现页面的图片所映射的地址在
Spring
Boot静态资源文件夹下找不到原來在这里是通过下面这段代码,将/store/**地址映射为getStorePath()得到的地址,在此记录以下
registry.addResourceHandler("/store/**").addResourceLocations(getStorePath());
@Override
public...
场景:部署的
Spring
boot
项目
,打成jar包,需要通过url直接访问jar外部
路径
的资源,本人的
Spring
boot为2.1.11.RELEASE。
一、配置
Web
MvcConfigurer,设置url访问静态资源映射
@Configuration
public class
Web
MvcConfig implements
Web
MvcConfigurer {