配置yml
spring:
servlet:
multipart:
max-file-size: 50MB
max-request-size: 50MB
files:
#下载文件虚拟路径(服务器ip)
staticUrl: http://192.168.1.142:8080
staticPath: /base/files/download
#文件真实路径
basePath: E:/real/file
#用户模块
user: /user/
映射虚拟路径与文件的真实路径
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/base/files/download/**")
.addResourceLocations("file:E:/real/file/");
}
}
路径工具类
@Component
@ConfigurationProperties(prefix = "files")
public class FileConfig {
public static final String user_file_path="user";
public static final String picture="picture";
private static String staticUrl;
private static String staticPath;
private static String basePath;
private static String user;
public static String getStaticUrl() {
return staticUrl;
}
public void setStaticUrl(String staticUrl) {
this.staticUrl = staticUrl;
}
public static String getStaticPath() {
return staticPath;
}
public void setStaticPath(String staticPath) {
this.staticPath = staticPath;
}
public static String getBasePath() {
return basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
public static String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public static String getFileUpLoadPath(String type,String key){
switch (type){
case FileConfig.user_file_path:
return getBasePath()+getUser()+key+"/";
default:
return getBasePath()+"/other/"+key+"/";
}
}
public static String getDownLoadPath(String type,String key,String fileName){
switch (type){
case FileConfig.user_file_path:
return getStaticUrl()+getStaticPath()+getUser()+key+"/"+fileName;
default:
return getStaticUrl()+getStaticPath()+"other"+key+"/"+fileName;
}
}
}
contorller上传方法
@RestController
@RequestMapping("/user")
public class UserController {
@PostMapping(value = "/uploadUserHeadPicture")
public Map<String,Object> uploadUserHead(@RequestParam("file") MultipartFile file){
String picDir=FileConfig.getFileUpLoadPath(FileConfig.user_file_path,FileConfig.picture);
File dir=new File(picDir);
if (!dir.exists()){
dir.mkdirs();
}
Map<String,Object> map=new HashMap<>();
String fileName= UUID.randomUUID().toString()+".jpg";
try {
file.transferTo(new File(picDir+fileName));
String downLoadPath=FileConfig.getDownLoadPath(FileConfig.user_file_path,FileConfig.picture,fileName);
map.put("message","success");
map.put("url",downLoadPath);
return map;
} catch (IOException e) {
map.put("message","error");
map.put("url","");
return map;
}
}
}
postman测试
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021041117392243.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNTc3NTQ4,size_16,color_FFFFFF,t_70#pic_center)
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210411173956512.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNTc3NTQ4,size_16,color_FFFFFF,t_70#pic_center)