if (file.isEmpty()) {
System.out.println("文件为空空");
}
String fileName = file.getOriginalFilename(); // 文件名
String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名
if(!suffixName.equals(".jpg") && !suffixName.equals(".png")){
return Result.error("请选择图片!!!");
}
String filePath = "D://temp-rainy//"; // 上传后的路径
fileName = UUID.randomUUID() + suffixName; // 新文件名
File dest = new File(filePath + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
String filename = "/temp-rainy/" + fileName;
return Result.success(filename);
@RestController
@RequestMapping(value = "/baseTest")
@CrossOrigin(origins = {"*","null"})
public class BaseTestController {
@PostMapping(value = "/base64Upload")
public Result uploadImage(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception{
if (file.isEmpty()) {
System.out.println("文件为空空");
}
String fileName = file.getOriginalFilename(); // 文件名
String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名
if(!suffixName.equals(".jpg") && !suffixName.equals(".png")){
return Result.error("请选择图片!!!");
}
String filePath = "D://temp-rainy//"; // 上传后的路径
fileName = UUID.randomUUID() + suffixName; // 新文件名
File dest = new File(filePath + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
String filename = "/temp-rainy/" + fileName;
return Result.success(filename);
}
}
参考文章:
- https://blog.csdn.net/u013380777/article/details/58603803
- https://blog.csdn.net/qq_41441312/article/details/80287498
- https://blog.csdn.net/ZHOU_VIP/article/details/88242482
- https://www.cnblogs.com/jueyushanlang/p/9243647.html
- https://blog.csdn.net/woainike/article/details/6620862
- https://blog.csdn.net/zerolaw/article/details/81083018
- https://blog.csdn.net/Peter_S/article/details/84951978
- https://blog.csdn.net/sdut406/article/details/85647982
原文:https://www.cnblogs.com/zhangkaifan/p/11029156.html