1,映射代码
package com.example.aliyuyin.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;
import java.io.File;
import java.io.IOException;
/**
* web 配置类
*
* @author fengshuonan
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${modelResourcePath}")
private String modelResourcePath;
@Value("${modelResourceUrl}")
private String modelResourceUrl;
/**
* 映射地址
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("file:E:/aliyuyin/");
//获取文件的存储目录
File modelResource = new File(modelResourcePath);
System.err.println(System.getProperty("user.dir"));
try {
//判断是windows系统还是linux系统,做不同路径的映射
String osName = System.getProperty("os.name").toLowerCase().substring(0,3);
if (osName.equals("win")){
registry.addResourceHandler(modelResourceUrl)
.addResourceLocations("file:"+modelResource.getCanonicalPath()+"\\");
}else if (osName.equals("lin")){
registry.addResourceHandler(modelResourceUrl)
.addResourceLocations("file:"+modelResource.getCanonicalPath()+"/");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.配置文件
modelResourcePath=/aliMp3
modelResourceUrl=/resource/**