diff --git a/spring-boot-file-upload/src/main/java/com/neo/controller/UploadController.java b/spring-boot-file-upload/src/main/java/com/neo/controller/UploadController.java index 13d3ab4676f52332eb3baae0bb9b599a801cd3d6..f72b2026a4a9874c9654cdd073d12a8edf2c4354 100644 --- a/spring-boot-file-upload/src/main/java/com/neo/controller/UploadController.java +++ b/spring-boot-file-upload/src/main/java/com/neo/controller/UploadController.java @@ -24,7 +24,7 @@ public class UploadController { @PostMapping("/upload") // //new annotation since 4.3 public String singleFileUpload(@RequestParam("file") MultipartFile file, - RedirectAttributes redirectAttributes) { + RedirectAttributes redirectAttributes) { if (file.isEmpty()) { redirectAttributes.addFlashAttribute("message", "Please select a file to upload"); return "redirect:uploadStatus"; @@ -33,16 +33,20 @@ public class UploadController { try { // Get the file and save it somewhere byte[] bytes = file.getBytes(); + Path dir = Paths.get(UPLOADED_FOLDER); Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename()); + // Create parent dir if not exists + if(!Files.exists(dir)) { + Files.createDirectories(dir); + } Files.write(path, bytes); - redirectAttributes.addFlashAttribute("message", - "You successfully uploaded '" + file.getOriginalFilename() + "'"); + "You successfully uploaded '" + file.getOriginalFilename() + "'"); } catch (IOException e) { + redirectAttributes.addFlashAttribute("message", "Server throw IOException"); e.printStackTrace(); } - return "redirect:/uploadStatus"; }