1、编写一个工具类PhotoUtils实现WebMvcConfigurer,然后重写addResourceHandlers方法即可
@Component
public class PhotoUtils implements WebMvcConfigurer {
//获取本地图片地址
@Value("${bmp.path:{null}}")
private String bmpPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* 访问路径:http://localhost:8086/image/2022001.png
* "/image/**" 为前端URL访问路径
* "file:" + bmpPath 是本地磁盘映射
*/
registry.addResourceHandler("/image/**").addResourceLocations("file:" + bmpPath);
}
}
2、然后在yml文件中配置本地图片的地址。(@Value(“${bmp.path:{null}}”)获取到的地址)
3、测试访问。访问地址就是项目地址+addResourceHandler方法的参数+bmpPath路径下面的照片名