From a04c4213054150d5691d66e5aacb52a04f8f5041 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Fri, 10 Feb 2017 18:13:32 +0530 Subject: [PATCH] BAEL - 326 - Add section about file upload with additional form data (#1105) --- .../com/baeldung/model/FormDataWithFile.java | 35 +++++++++++++ .../web/controller/FileUploadController.java | 20 +++++++ .../webapp/WEB-INF/view/fileUploadForm.jsp | 52 +++++++++++++++++++ .../webapp/WEB-INF/view/fileUploadView.jsp | 43 +++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 spring-mvc-java/src/main/java/com/baeldung/model/FormDataWithFile.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/FormDataWithFile.java b/spring-mvc-java/src/main/java/com/baeldung/model/FormDataWithFile.java new file mode 100644 index 0000000000..29925a592d --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/model/FormDataWithFile.java @@ -0,0 +1,35 @@ +package com.baeldung.model; + +import org.springframework.web.multipart.MultipartFile; + +public class FormDataWithFile { + + private String name; + private String email; + private MultipartFile file; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public MultipartFile getFile() { + return file; + } + + public void setFile(MultipartFile file) { + this.file = file; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java index 61bccb21aa..b357b9270f 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java @@ -2,11 +2,14 @@ package com.baeldung.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; +import com.baeldung.model.FormDataWithFile; + @Controller public class FileUploadController { @@ -29,4 +32,21 @@ public class FileUploadController { modelMap.addAttribute("files", files); return "fileUploadView"; } + + @RequestMapping(value = "/uploadFileWithAddtionalData", method = RequestMethod.POST) + public String submit(@RequestParam final MultipartFile file, @RequestParam final String name, @RequestParam final String email, final ModelMap modelMap) { + + modelMap.addAttribute("name", name); + modelMap.addAttribute("email", email); + modelMap.addAttribute("file", file); + return "fileUploadView"; + } + + @RequestMapping(value = "/uploadFileModelAttribute", method = RequestMethod.POST) + public String submit(@ModelAttribute final FormDataWithFile formDataWithFile, final ModelMap modelMap) { + + modelMap.addAttribute("formDataWithFile", formDataWithFile); + return "fileUploadView"; + } + } diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp index 1414b824ff..41b7c09629 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -49,6 +49,58 @@ + +
+ +

Fill the Form and Select a File (@RequestParam)

+ + + + + + + + + + + + + + + + + + + +
Name
Email
Select a file to upload
+ +
+ +
+ +

Fill the Form and Select a File (@ModelAttribute)

+ + + + + + + + + + + + + + + + + + + +
Name
Email
Select a file to upload
+ +
diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp index d6f748c6af..696af4c4aa 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp @@ -32,5 +32,48 @@ +
+ +

Submitted File with Data (@RequestParam)

+ + + + + + + + + + + + + + + + + +
Name :${name}
Email :${email}
OriginalFileName :${file.originalFilename}
Type :${file.contentType}
+ +
+ +

Submitted File with Data (@ModelAttribute)

+ + + + + + + + + + + + + + + + + +
Name :${formDataWithFile.name}
Email :${formDataWithFile.email}
OriginalFileName :${formDataWithFile.file.originalFilename}
Type :${formDataWithFile.file.contentType}
\ No newline at end of file