Springboot 配置静态资源映射

配置文件配置映射

spring:  
  mvc:
  	#图片访问地址路径
    static-path-pattern: /file/**
    throw-exception-if-no-handler-found: true
  resources: #静态资源映射地址
    static-locations: classpath:/METAINF/resources/,classpath:/resources/,
    classpath:/static/,classpath:/assets/,file:${fileStorage.path}
fileStorage: #本地文件存储地址
  path: E:\filePath\
  

实现WebMvcConfigurer接口



@Configuration
public class FileConfig implements WebMvcConfigurer  {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
	 registry.addResourceHandler("/assets/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/assets/");
	 registry.addResourceHandler("/head/*").addResourceLocations("File:E:/headFile/");

}

classPath为
在这里插入图片描述

图片通过存储地址为:E:\filePath\1.jpg
图片访问地址为: http://ip:port/file/1.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,静态资源映射是由Spring Boot默认提供的配置完成的。Spring Boot将所有访问映射到以下目录:classpath:/static、classpath:/public、classpath:/resources和classpath:/META-INF/resources。如果这些目录中有多张同名的图片,访问地址的优先级顺序为:META-INF/resources > resources > static > public。\[2\] 如果我们请求一个以".html"结尾的静态资源Spring Boot会按照优先级高的文件夹先查找,然后再查找优先级低的文件夹,直到找到指定的静态资源为止。\[3\] 例如,在src/main/resources/static目录下创建了一个hello.html文件,当我们启动Spring Boot并访问"http://localhost:8080/hello.html"时,就会显示该hello.html页面。\[3\] 总结来说,Spring Boot静态资源映射提供了默认配置,可以直接将静态资源放置在指定的目录中,无需进行额外的配置即可访问这些静态资源。\[1\] #### 引用[.reference_title] - *1* *3* [Spring Boot静态资源映射](https://blog.csdn.net/weixin_64842782/article/details/125089662)[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] - *2* [springboot静态资源映射](https://blog.csdn.net/ChOLg/article/details/102800978)[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、付费专栏及课程。

余额充值