文章目录
- 新建static文件夹,存储图片等资源
- 方式一 Java编码定义
- 方式二 配置中定义
新建static文件夹,存储图片等资源

 项目版本信息:SpringBoot版本为2.3.3.RELEASE,JDK1.8
方式一 Java编码定义
配置类实现WebMvcConfigurer接口,然后实现该接口的addResourceHandlers方法。
- 关键代码:registry.addResourceHandler(“/static/**”).addResourceLocations(“classpath:/static/”);
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
    
    @Value("${photo.dir}")
    private String realPath;
    /**
     * 添加静态资源访问
     *
     * @param registry 类位置:
     * @Configuration public class MyWebMvcConfigurer implements WebMvcConfigurer
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 示例1
        registry.addResourceHandler("/static/**").          
                addResourceLocations("classpath:/static/");
         // 示例2       
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:"+realPath);
    }
}
在application.properties中定义realPath即图片存储的实际位置。
# 端口号
server.port=8081
# 设置context-path
server.servlet.context-path=/studyboot2023
# 静态资源存放位置路径(以/结尾)
photo.dir=/Users/wzp/IdeaProjects/SpringBootReview202305/study01/src/main/resources/static/photo/
重启项目,在浏览器中输入
 http://localhost:8081/studyboot2023/static/photo/xidianlogonew.png
 或
 http://localhost:8081/studyboot2023/upload/xidianlogonew.png
 即可看到classpath:/static/下存放的图片资源。
方式二 配置中定义
无需配置类,直接在application.properties中定义
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/
过滤规则为/static/**,静态资源位置为classpath:/static
 重启项目后访问:http://localhost:8081/studyboot2023/static/photo/xidianlogonew.png










![[优雅的面试]MySQL与Redis双写一致性方案](https://img-blog.csdnimg.cn/img_convert/0fe744b099af749d7152388df99fefce.png)








