上传文件(图片头像)配置静态资源路径static-locations、static-path-pattern

建议参考原文,格式清晰:配置静态资源路径static-locations、static-path-pattern - 简书 (jianshu.com)


实际开发静态资源 html、js、图片 肯定是放在各自文件夹下面的 参考链接

一、浅析 static-locations、static-path-pattern

  • spring.mvc.static-path-pattern 从 WebMvcAutoConfiguration -> WebMvcAutoConfigurationAdapter -> WebMvcProperties 中可以看出默认是 /** ,根据官网的描述和实际效果,可以理解为静态文件URL匹配头,也就是静态文件的URL地址开头。

private String staticPathPattern = "/**";
  • spring.web.resources.static-locations 从 WebMvcAutoConfiguration -> WebMvcAutoConfigurationAdapter -> WebProperties -> Resources 中可以看出默认是 "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/",根据官网的描述和实际效果,可以理解为实际静态文件地址,也就是静态文件URL后,匹配的实际静态文件。

 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
        private String[] staticLocations;
        private boolean addMappings;
        private boolean customized;
        private final WebProperties.Resources.Chain chain;
        private final WebProperties.Resources.Cache cache;
​
        public Resources() {
            this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
            this.addMappings = true;
            this.customized = false;
            this.chain = new WebProperties.Resources.Chain();
            this.cache = new WebProperties.Resources.Cache();
        }
​
        public String[] getStaticLocations() {
            return this.staticLocations;
        }
​
        public void setStaticLocations(String[] staticLocations) {
            this.staticLocations = this.appendSlashIfNecessary(staticLocations);
            this.customized = true;
        }

二、 项目根目录下新建静态文件夹

demo地址

  • SystemData/UserData/Avatar/p1.png

root-static.png

1、 application.properties

  • 分别设置 spring.mvc.static-path-pattern spring.web.resources.static-locations

  • 请注意static-locations中的file:SystemData就是映射本地文件

spring.mvc.static-path-pattern=/SystemData/**
spring.web.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources,file:SystemData

proone.png

2、效果展示

showOne.png

三、static 文件下 继续分文件

demo 地址

config2.png

error.png

可以访问: http://localhost:8080/JS/1.js http://localhost:8080/Image/1.png http://localhost:8080/JS/1.html

ok.png

3、 1.js 1.png 1.html 和 2.js 一样直接访问

设置 spring.web.resources.static-locations

  • classpath:/static/JS

  • classpath:/static/Image

  • classpath:/static/HTML

spring.web.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources,classpath:/static/JS,classpath:/static/Image,classpath:/static/HTML

config.png

3.1 、http://localhost:8080/1.jshttp://localhost:8080/1.pnghttp://localhost:8080/1.html 可以直接访问了

ok.png

四、需要设置多个地址为静态资源目录

demo地址

这样的配置,可以说最简单且粗暴,但是灵活性差一点点:

URL响应地址只能为一项,也就是spring.mvc.static-path-pattern配置只能写一项。 这意味着,按我上文设置了/SystemData/为URL匹配,就不能设置第二个/resources/这样的配置为第二静态目录。

many.png

写一个配置类,实现静态资源的文件夹方法很多。比如: 继承于WebMvcConfigurationSupport父类,并实现addResourceHandlers方法。 引用WebMvcConfigurer接口,并实现addInterceptors方法

1、实现一个一个配置类,并继承WebMvcConfigurationSupport,实现addResourceHandlers方法,并打上@Configuration注解,使其成为配置类:

package com.example.springboot02staticconfig03.Config;
​
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
​
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
​
    // System.getProperty("user.dir")  当前程序所在目录
    static final String IMG_PATH = System.getProperty("user.dir")+"/SystemData/";
    static final String IMG_PATH_TWO = System.getProperty("user.dir")+"/Test/";
​
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 静态资源映射
        registry.addResourceHandler("/SystemData/**").addResourceLocations("file:"+IMG_PATH);
        registry.addResourceHandler("/Test/**").addResourceLocations("file:"+IMG_PATH_TWO);
        super.addResourceHandlers(registry);
    }
}

config.png

2、实现效果

现在我们就来配置。 最终效果很简单,我想要的效果(两组同时):

浏览器输入:http://localhost:8080/SystemData/UserData/Avatar/1.png 可以直接访问项目文件下的:/SystemData/UserData/Avatar/1.png,

浏览器输入:http://localhost:8080/Test/UserData/Avatar/2.png 可以直接访问项目文件下的:/Test/UserData/Avatar/2.png,

1.png

2.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot项目中,可以使用`spring.resources.static-locations`属性来指定查找本地静态文件的位置。该属性可以在项目的配置文件(如application.properties或application.yml)中进行配置。通过配置该属性,可以告诉Spring Boot在哪些目录下查找静态文件。 例如,如果想要在项目的根目录下的static文件夹和public文件夹中查找静态文件,可以在配置文件中添加以下配置: ``` spring.resources.static-locations=classpath:/static/,classpath:/public/ ``` 这样,当访问静态资源时,Spring Boot会在这两个目录下查找对应的文件。如果找到匹配的文件,就会返回给客户端。 需要注意的是,`spring.resources.static-locations`属性的值可以是一个或多个目录路径,多个路径之间使用逗号进行分隔。路径可以是相对路径或绝对路径,也可以是classpath路径。 希望这个解答对你有帮助!\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [spring.mvc.static-path-patternspring.resources.static-locations](https://blog.csdn.net/baidu_38225647/article/details/109464223)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Spring Boot配置静态资源的地址与访问路径spring.mvc.static-path-patternspring.web.resources.static-...](https://blog.csdn.net/weixin_38924500/article/details/109739021)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值