SpringBoot静态资源文件(图片)映射

1.定义配置类WebConfig

package com.example.demo03.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
         // 访问地址:如http://localhost:8081/api/file/1.jpg
        registry.addResourceHandler("/api/file/**") 
                // 访问http://localhost:8081/api/file/1.jpg 会自动找E:\demo03\src\main\resources\static\images\ 路径下名为1.jpg图片
                .addResourceLocations("file:" + "E:\\demo03\\src\\main\\resources\\static\\images\\");
    }
}

在浏览器中访问http://localhost:8081/api/file/1.jpg就可以看到图片

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值