@ResponseBody
@RequestMapping(value="upload")
public void testUpload(@RequestParam("file") MultipartFile file) throws IOException {
Map<String, Object> map = new HashMap<>();
if (file.length == 0) {
Resp res = Resp.getNew().fail("文件内容为空,请重新选择文件!");
return res;
}
String fileExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String fileName = "qqq" + fileExtension;
String path ="D:/imgs/";
//检查该路径对应的目录是否存在. 如果不存在则创建目录
File dir=new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
String filePath = path + fileName;
System.out.println("filePath: "+filePath);
//保存文件
File dest = new File(filePath);
if (!(dest.exists())) {
file.transferTo(dest);
}
}
删除本地图片
try {
String filePath = filePathAndName;//路径
System.out.println(filePath);
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete();
} catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}