springboot 资源文件映射问题

1、需求来源

    图片的上传后并显示在页面

2、技术基础

使用springboot,此处楼主使用1.5.10.RELEASE。(2.0版本也行,已测)

3、思路方案

1)文件存的基本思路

楼主上传的文件放在F盘的某个目录下,数据库存的是某目录下的文件路径,此处可看做是图片路径。即F:\logs\platLogo\1552294228766.png,图片存的绝对路径。

2)文件取的基本思路

项目部署后,我们不知道项目具体的文件存放位置,也不能使用绝对路径来获取文件,这里就难住了我。于是楼主就想,怎么将绝对路径转化为相对路径,通过项目访问就可以获取文件。

4、解决措施

查看资料,发现springboot可以配置文件的映射。如下面代码:

@Configuration
public class WebMvcConfg extends WebMvcConfigurerAdapter  {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/web/**").addResourceLocations("file:F:/logs");
        super.addResourceHandlers(registry);
    }

}

简单说明:"file:F:/logs" 表示本地文件的路径,"/web/**"表示项目访问的路径,即http://localhost:8900/web/ 。如我要访问本地的文件,在浏览器上输入:F:\logs\platLogo\1552294228766.png,就可以访问。通过springboot文件映射后,可以这样访问:http://localhost:8900/web/platLogo/1552294228766.png.这样我们就可以在页面显示图片。

5、代码共享

1)向前端返回实体

@ApiOperation(value="获取平台信息/播放插件版本号")
	@GetMapping(value = "/getPlatMsg")
    @ApiImplicitParam(name = "type", value = "类型:2 表示 播放插件,3 表示logo", required = true)
	public ApiResponse<SysconfigPlugVo> getPlatMsg(String type) {
	    if (StrUtil.isBlank(type)){
	        return new ApiResponse<SysconfigPlugVo>().error("类型不能为空");
        }
        SysconfigPlugVo item = sysconfigPlugService.getDownloadParam(type);
		if (item != null && StrUtil.isBlank(item.getPath())){
			return new ApiResponse().error("请上传logo图片");
		}
		return new ApiResponse().ok().setData(item);
	}

2)前端展示

前端使用的vue, 这里的api是前端配置访问后端的接口的ip+端口,web是映射的路径

 getPlatformMsg(){
      this.$ajax.get(`/api/sysconfigParam/getPlatMsg`, {'params': {type: 3}}).then(res => {
        if (res.data.code === this.hdConst.OK) {
          let param = res.data.data
          let imgUrl = '/api/web/'+param.path
          let imgName = param.name
        }
      })
    },

 

6、心得

    此处的关键是文件路径的映射,映射环境配置好了,返回路径到前端,前端可以根据路径实现图片的显示或者文件的下载。小小例子,记录一下。

 

7、项目地址(多种图片显示总结)

https://github.com/krycai/Allen-projects/tree/master/allen-upload

 

 

 

回答: 在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.youkuaiyun.com/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 ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值