你好
我觉得 我很帅
// 访问外部静态资源文件
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
//windows系统下访问路径
private static final String filePathWindow = "C:\\img\\";
//Linux系统下访问路径
private static final String filePathLinux = "/user/img/";
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// 获取操作系统名称
String os = System.getProperty("os.name");
//如果是Windows系统
if (os.toLowerCase().startsWith("win")) {
registry.addResourceHandler("/image/**")
// /image/**: 表示在磁盘filePathWindow目录下的所有资源会被解析为以下的路径
.addResourceLocations("file:" + filePathWindow);
} else {
//linux 和 mac
registry.addResourceHandler("/image/**")
.addResourceLocations("file:" + filePathLinux);
}
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.addResourceLocations("classpath:/templates/");
super.addResourceHandlers(registry);
}
/*
跨域配置
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.allowCredentials(true)
.allowedHeaders("*")
.maxAge(3600);
}
}