非常简单,首先配置yml文件
server:
port: 8080
servlet:
context-path: /demo/
picture:
# 图片访问路径
show: /show/**
# 本地映射,图片真实存放路径
up-path: D:/MyPhoto/picture/
然后创建WebConfig实现WebMvcConfigurer
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Description TODO
* @Author admin
* @Date 2020/12/10
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${picture.show}")
private String showPath;
@Value("${picture.up-path}")
private String upPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(showPath).addResourceLocations("file:" + upPath);
}
}
这样就可以了,我们按照yml配置的的存储路径放两张照片试一哈。


启动项目,打开浏览器,效果如下:


本文介绍了如何在SpringBoot应用中配置URL以访问本地图片,通过修改yml文件和创建WebConfig实现WebMvcConfigurer接口,实现了从指定存储路径加载图片并展示的功能。
2844





