上传图片
MultipartFile file接收图片文件,
获取后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
重新生成图片名
fileName = UUID.randomUUID()+suffixName;
定义文件存储路径,如果目录不存在,新建目录
File saveFile = new File(filePath,fileName);
if (!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs();
}
try {
//将文件保存指定目录
file.transferTo(new File(filePath + fileName));
String url = "http://"+req.getRemoteHost()+":"+req.getServerPort()+path+"/"+ format + "/" + fileName;
return url;
} catch (Exception e) {
e.printStackTrace();
return "false";
}
静态映射
访问本地路径资源时,静态映射,在yml文件中配置
spring:
resources:
static-locations: file:${file.upload-path} #磁盘 静态路径映射
file:
upload-path: E:/HBuilderWorkSpace/uploadImg



