1、前端直接引用本地图片,图片加载失败
具体报错信息如下:
Not allowed to load local resource
不允许加载本地资源
2、针对以上问题,只需要利用拦截器将本机地址映射成url路径就行
具体代码如下
@Configuration
public class FileConfig implements WebMvcConfigurer {
@Value("${ATTACHMENT_PATH}")
private String attachmentPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/image/**") //虚拟url路径
//.addResourceLocations("file:D:/images/");
.addResourceLocations("file:" + attachmentPath); //真实本地路径
}
}
图片访问地址为:http://localhost:8088/image/aaa.png