mirror of
https://github.com/ityouknow/spring-boot-examples.git
synced 2026-06-08 10:03:06 +08:00
fix file upload err cause by parent directory not exists
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user