Springboot添加静态资源映射addResourceHandlers

springboot使用的时候 需要设置静态资源以及目录 通过addResourceHandlers方法可以实现相关资源文件的配置:

addResourceHandler("/qy/**") 所有/qy/开头的请求 都会去后面配置的路径下查找资源

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // 添加文档对应的映射
    registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    // 添加对应的映射
    registry.addResourceHandler("/qy/**").addResourceLocations("classpath:/static/qy/");
    super.addResourceHandlers(registry);
}
回答: 在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、付费专栏及课程。

余额充值