Skip to content

Commit a04c421

Browse files
vivekkr12pivovarit
authored andcommitted
BAEL - 326 - Add section about file upload with additional form data (eugenp#1105)
1 parent 22d2604 commit a04c421

4 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.model;
2+
3+
import org.springframework.web.multipart.MultipartFile;
4+
5+
public class FormDataWithFile {
6+
7+
private String name;
8+
private String email;
9+
private MultipartFile file;
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
19+
public String getEmail() {
20+
return email;
21+
}
22+
23+
public void setEmail(String email) {
24+
this.email = email;
25+
}
26+
27+
public MultipartFile getFile() {
28+
return file;
29+
}
30+
31+
public void setFile(MultipartFile file) {
32+
this.file = file;
33+
}
34+
35+
}

‎spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import org.springframework.stereotype.Controller;
44
import org.springframework.ui.ModelMap;
5+
import org.springframework.web.bind.annotation.ModelAttribute;
56
import org.springframework.web.bind.annotation.RequestMapping;
67
import org.springframework.web.bind.annotation.RequestMethod;
78
import org.springframework.web.bind.annotation.RequestParam;
89
import org.springframework.web.multipart.MultipartFile;
910

11+
import com.baeldung.model.FormDataWithFile;
12+
1013
@Controller
1114
public class FileUploadController {
1215

@@ -29,4 +32,21 @@ public String submit(@RequestParam("files") final MultipartFile[] files, final M
2932
modelMap.addAttribute("files", files);
3033
return "fileUploadView";
3134
}
35+
36+
@RequestMapping(value = "/uploadFileWithAddtionalData", method = RequestMethod.POST)
37+
public String submit(@RequestParam final MultipartFile file, @RequestParam final String name, @RequestParam final String email, final ModelMap modelMap) {
38+
39+
modelMap.addAttribute("name", name);
40+
modelMap.addAttribute("email", email);
41+
modelMap.addAttribute("file", file);
42+
return "fileUploadView";
43+
}
44+
45+
@RequestMapping(value = "/uploadFileModelAttribute", method = RequestMethod.POST)
46+
public String submit(@ModelAttribute final FormDataWithFile formDataWithFile, final ModelMap modelMap) {
47+
48+
modelMap.addAttribute("formDataWithFile", formDataWithFile);
49+
return "fileUploadView";
50+
}
51+
3252
}

‎spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,58 @@
4949
</table>
5050

5151
</form:form>
52+
53+
<br />
54+
55+
<h3>Fill the Form and Select a File (<code>@RequestParam</code>)</h3>
56+
57+
<form:form method="POST" action="/spring-mvc-java/uploadFileWithAddtionalData" enctype="multipart/form-data">
58+
59+
<table>
60+
<tr>
61+
<td>Name</td>
62+
<td><input type="text" name="name" /></td>
63+
</tr>
64+
<tr>
65+
<td>Email</td>
66+
<td><input type="text" name="email" /></td>
67+
</tr>
68+
<tr>
69+
<td>Select a file to upload</td>
70+
<td><input type="file" name="file" /></td>
71+
</tr>
72+
<tr>
73+
<td><input type="submit" value="Submit" /></td>
74+
</tr>
75+
</table>
76+
77+
</form:form>
78+
79+
<br />
80+
81+
<h3>Fill the Form and Select a File (<code>@ModelAttribute</code>)</h3>
82+
83+
<form:form method="POST" action="/spring-mvc-java/uploadFileModelAttribute" enctype="multipart/form-data">
84+
85+
<table>
86+
<tr>
87+
<td>Name</td>
88+
<td><input type="text" name="name" /></td>
89+
</tr>
90+
<tr>
91+
<td>Email</td>
92+
<td><input type="text" name="email" /></td>
93+
</tr>
94+
<tr>
95+
<td>Select a file to upload</td>
96+
<td><input type="file" name="file" /></td>
97+
</tr>
98+
<tr>
99+
<td><input type="submit" value="Submit" /></td>
100+
</tr>
101+
</table>
102+
103+
</form:form>
52104

53105
</body>
54106

‎spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,48 @@
3232
</tr>
3333
</c:forEach>
3434
</table>
35+
<br />
36+
37+
<h2>Submitted File with Data (<code>@RequestParam</code>)</h2>
38+
<table>
39+
<tr>
40+
<td>Name :</td>
41+
<td>${name}</td>
42+
</tr>
43+
<tr>
44+
<td>Email :</td>
45+
<td>${email}</td>
46+
</tr>
47+
<tr>
48+
<td>OriginalFileName :</td>
49+
<td>${file.originalFilename}</td>
50+
</tr>
51+
<tr>
52+
<td>Type :</td>
53+
<td>${file.contentType}</td>
54+
</tr>
55+
</table>
56+
57+
<br />
58+
59+
<h2>Submitted File with Data (<code>@ModelAttribute</code>)</h2>
60+
<table>
61+
<tr>
62+
<td>Name :</td>
63+
<td>${formDataWithFile.name}</td>
64+
</tr>
65+
<tr>
66+
<td>Email :</td>
67+
<td>${formDataWithFile.email}</td>
68+
</tr>
69+
<tr>
70+
<td>OriginalFileName :</td>
71+
<td>${formDataWithFile.file.originalFilename}</td>
72+
</tr>
73+
<tr>
74+
<td>Type :</td>
75+
<td>${formDataWithFile.file.contentType}</td>
76+
</tr>
77+
</table>
3578
</body>
3679
</html>

0 commit comments

Comments
 (0)