@PostMapping("/upload")
public String upload(MultipartFile uploadFile, HttpServletRequest req) {
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
File folder = new File(realPath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
}
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
uploadFile.transferTo(new File(folder, newName));
String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + "/" + newName;
return filePath;
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败!";
}
Java文件上传处理接口
最新推荐文章于 2024-08-26 15:11:36 发布