springboot如何通过URL方式访问外部资源
作者:木子锦520 时间:2022-02-07 01:10:15
springboot通过URL方式访问外部资源
遇到这个问题时翻阅百度,无外乎就是两种方式
第一种
在springboot 2.1.8中该方法已过时
第二种
这个方法是可以实现通过url访问到指定目录下的文件,但是使用spring.resources.static-locations这个配置会覆盖掉SpringBoot默认的静态资源文件夹,项目的静态资源全都无法访问,而spring.mvc.static-path-pattern 默认值就是/** 无须配置。
解决方法,在spring.resources.static-locations 加入自定义要访问的文件夹路径,必须是file:开头,并且加上默认的四个静态资源路径就可以访问项目和自定义静态资源了。
自定义静态资源路径为D:file/
spring那里报错是因为上面已经存在spring层,这里加上为了方便演示
自定义静态资源文件夹下的文件
通过url访问下载.JPG这张图片
同时项目也能访问到
springboot通过URL访问本地文件
@Configuration
public class MyWebMvcConfigurer extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 访问相对路径
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
// 访问绝对路径 访问路径和 存放路径可以自定义,建议存放路径放到配置文件中
registry.addResourceHandler("/img/**").addResourceLocations("file:D:/img/");
super.addResourceHandlers(registry);
}
}
localhost:8080/assets/文件名.后缀
localhost:8080/img/文件名.后缀
来源:https://blog.csdn.net/weixin_43224694/article/details/103175992
标签:springboot,URL,访问资源
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java8中CompletableFuture类的使用
2022-06-28 17:39:43
java多线程Thread的实现方法代码详解
2022-01-03 01:02:28
java实现捕鱼达人游戏
2023-11-23 21:28:59
![](https://img.aspxhome.com/file/2023/6/70926_0s.jpg)
java打印指定年月的日历
2023-11-11 19:21:19
![](https://img.aspxhome.com/file/2023/2/59062_0s.jpg)
Android 自定义View实现多节点进度条功能
2022-05-04 17:13:37
![](https://img.aspxhome.com/file/2023/5/98665_0s.png)
Java事件监听机制讲解
2022-09-18 00:41:11
深入理解java final不可变性
2023-02-11 20:17:27
![](https://img.aspxhome.com/file/2023/4/62124_0s.jpg)
java 制作验证码并进行验证实例详解
2023-03-18 11:27:56
Java虚拟机内存区域划分详解
2023-06-20 22:04:24
![](https://img.aspxhome.com/file/2023/6/57516_0s.png)
一文详解Spring如何控制Bean注入的顺序
2022-09-10 18:33:22
![](https://img.aspxhome.com/file/2023/7/70947_0s.jpg)
浅谈如何在项目中使用Spring Cloud Alibaba Sentinel组件
2022-07-12 14:32:17
![](https://img.aspxhome.com/file/2023/8/96088_0s.png)
带你了解Java Maven的打包操作
2022-08-03 15:23:33
![](https://img.aspxhome.com/file/2023/7/68537_0s.jpg)
Java文件快速copy复制实例代码
2021-05-27 12:25:22
![](https://img.aspxhome.com/file/2023/7/66077_0s.png)
详谈jvm--Java中init和clinit的区别
2022-01-10 10:35:22
Java微信公众号开发之通过微信公众号获取用户信息
2021-12-16 08:19:19
![](https://img.aspxhome.com/file/2023/3/88043_0s.png)
Mybatis plus中的like查询问题
2022-03-18 09:14:30
基于Java信号量解决死锁过程解析
2023-05-13 22:23:02
Spring Bean初始化及销毁多种实现方式
2023-06-10 13:14:01
![](https://img.aspxhome.com/file/2023/0/71620_0s.jpg)
Flutter 通过Clipper实现各种自定义形状的示例代码
2023-06-19 14:25:11
![](https://img.aspxhome.com/file/2023/7/59397_0s.jpg)
Java LinkedList实现班级信息管理系统
2021-06-27 04:00:11
![](https://img.aspxhome.com/file/2023/7/84657_0s.jpg)