1.config包下,创建配置类:
2.WebConfig类:
package com.hongyi.cms.facereport.config;
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;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${upload.path}")
private String uploadPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* 访问路径:http://localhost:93/facereport/static/captured/daping_report/captured_images/face/788001414206074880/2021/1/27/cj_142207194912440320.png
* "/facereport/static/captured/**" 为前端URL访问路径
* "file:" + uploadPath 是本地磁盘映射
*/
registry.addResourceHandler("/facereport/static/captured/**").addResourceLocations("file:" + uploadPath);
}
}
3.@Value("${upload.path}")
在yml里配置:
4.前端url访问:http://localhost:93/facereport/static/captured/daping_report/captured_images/face/788001414206074880/2021/1/27/1.jpg
成功映射,并显示: