Springboot中的资源映射——路径

classpath

引用自
https://www.cnblogs.com/daihu/p/13851519.html

一、classpath指向了哪里

1、用maven构建(build)项目时,默认的classpath指向target/classes/

2、用maven打包(package)项目时,默认的classpath指向war内部的WEB-INF/classes/

二、如果获取springboot项目默认的classpath

// 获取classpath
String classpath = ResourceUtils.getURL("classpath:").getPath();

路径写法

在这里插入图片描述
指需要定位到绝对路径时,必须加file

静态资源映射路径配置

引用自
https://www.cnblogs.com/pomer-huang/archive/2018/09/12/pomer.html

  • 无任何前缀 -> “文档根目录”(一般指代 src/main/webapp 目录), 例如 localhost:8080/index.html 定位至 src/main/webapp/static/index.html
  • 存在前缀 classpath -> 类路径(一般指代 src/main/resources 目录)
  • 存在前缀 file:// -> 文件系统路径(“绝对路径”)

案例

无前缀,访问src目录下的webapp

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Spring Boot,可以使用yml文件来配置静态资源映射。具体配置方式如下: 在application.yml文件添加以下内容: ```yaml img-filepath: /path/to/img/folder/ video-filepath: /path/to/video/folder/ ``` 其,`/path/to/img/folder/`是存放图片的文件夹路径,`/path/to/video/folder/`是存放视频的文件夹路径。 然后,在启动类使用`@Value`注解来获取配置文件路径: ```java @SpringBootApplication public class StudyApplication implements WebMvcConfigurer { @Value("${img-filepath}") private String imgPath; @Value("${video-filepath}") private String videoPath; // 省略其他代码 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/img/**").addResourceLocations("file:" + imgPath); registry.addResourceHandler("/play/**").addResourceLocations("file:" + videoPath); } // 省略其他代码 } ``` 这样,当访问`http://localhost:8080/img/0a3b3288-3446-4420-bbff-f263d0c02d8e.jpg`时,就会去`/path/to/img/folder/`下找到对应的图片。同理,访问`http://localhost:8080/play/`时,会去`/path/to/video/folder/`下找到对应的视频。 #### 引用[.reference_title] - *1* *2* *3* [springboot静态资源映射配置](https://blog.csdn.net/m0_62317155/article/details/128622300)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值