From d24f73002a2c21f39ffb012fb96f85fb64020259 Mon Sep 17 00:00:00 2001 From: ACHRAF TAITAI <43656331+achraftt@users.noreply.github.com> Date: Thu, 4 Aug 2022 10:49:11 +0200 Subject: [PATCH] Load Image With Spring Boot and Thymeleaf (#12486) * Load Image With Spring Boot and Thymeleaf * Load Image With Spring Boot and Thymeleaf --- .../imageupload/UploadController.java | 31 +++++++++++++++++++ .../src/main/resources/application.properties | 4 ++- .../templates/imageupload/index.html | 25 +++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/imageupload/UploadController.java create mode 100644 spring-web-modules/spring-thymeleaf-5/src/main/resources/templates/imageupload/index.html diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/imageupload/UploadController.java b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/imageupload/UploadController.java new file mode 100644 index 0000000000..d3ccc82323 --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/imageupload/UploadController.java @@ -0,0 +1,31 @@ +package com.baeldung.thymeleaf.imageupload; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +@Controller public class UploadController { + + public static String UPLOAD_DIRECTORY = System.getProperty("user.dir") + "/uploads"; + + @GetMapping("/uploadimage") public String displayUploadForm() { + return "imageupload/index"; + } + + @PostMapping("/upload") public String uploadImage(Model model, @RequestParam("image") MultipartFile file) throws IOException { + StringBuilder fileNames = new StringBuilder(); + Path fileNameAndPath = Paths.get(UPLOAD_DIRECTORY, file.getOriginalFilename()); + fileNames.append(file.getOriginalFilename()); + Files.write(fileNameAndPath, file.getBytes()); + model.addAttribute("msg", "Uploaded images: " + fileNames.toString()); + return "imageupload/index"; + } +} \ No newline at end of file diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/resources/application.properties b/spring-web-modules/spring-thymeleaf-5/src/main/resources/application.properties index b09232bb1b..7a5b9c207d 100644 --- a/spring-web-modules/spring-thymeleaf-5/src/main/resources/application.properties +++ b/spring-web-modules/spring-thymeleaf-5/src/main/resources/application.properties @@ -1 +1,3 @@ -#spring.thymeleaf.prefix=classpath:/templates-2/ \ No newline at end of file +#spring.thymeleaf.prefix=classpath:/templates-2/ +spring.servlet.multipart.max-file-size = 5MB +spring.servlet.multipart.max-request-size = 5MB \ No newline at end of file diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates/imageupload/index.html b/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates/imageupload/index.html new file mode 100644 index 0000000000..e129a123e9 --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates/imageupload/index.html @@ -0,0 +1,25 @@ + + +
+ + + +