Reputation: 1566
I know that there are plenty of questions covering the topic, but I cannot figure out how to achieve the following requirement.
I would like to upload a list of files, each one containing some extra information. In the java world this would mean the following:
@NoArgsConstructor
@Getter
public class SkillsVerificationData {
String type; // this information is related to the file
MultipartFile file;
}
Question 1:
Is this possible for a RestController
to achieve such a mapping using a wrapper object? (See first answer of the referenced question- @ModelAttribute)
Question 2: Using the following controller method answered in the question referenced above
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = { "multipart/form-data" })
public void upload(@RequestPart("type") @Valid String type,
@RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
}
I assume that it applies for a single file. How should the request parts be defined/described to achieve uploading a List<SkillsVerificationData>
or SkillsVerificationData
[] ?
Note that the client sends the information using FormData
.
Thanks in advance!
Upvotes: 0
Views: 5040
Reputation: 8011
If i understand your question correctly, you want to upload a list of files along with certain information specific to each file. I can correlate with an example to bring more clarity. A user wants to upload a list of candidates' technical profiles or resumes along with candidate information. If that is the case, you can create an array of MultiPart file and a json structure which contains information about each unique fileName, with unique id and candidate information in a json structure. If that is the requirement, you can refer below the code.
@PostMapping(
value = "/multiUpload",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<?> uploadingMultipleFiles(
@RequestParam("files") MultipartFile[] uploadingFiles, @RequestPart(value = "emps", required = true) String empJsonTxt) {
System.out.println("EMP as Json String = " + empJsonTxt);
//Process the empJsonTxt
ObjectMapper objectMapper = new ObjectMapper();
try {
Employee emp = objectMapper.readValue(empJsonTxt, Employee.class);
System.out.println("Now emp = " + emp);
} catch (IOException e) {
e.printStackTrace();
}
for (MultipartFile uploadedFile : uploadingFiles) {
System.out.println("Uploaded File Name = " + uploadedFile.getOriginalFilename());
File file = new File("E:/sure-delete/" + uploadedFile.getOriginalFilename());
//Upload functionality
try {
uploadedFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
}
return ResponseEntity.ok("All Files uploaded successfully ...");
}
In this case empJsonTxt is a string of json contents which contains all the required information about the employees and their resume/profile. Multipart files will be used only for uploading. However, you can extrapolate this part to make suitable to your requirement. This is doable, there may be many more good approaches also.
Upvotes: 4