1

Hello Friends,

I am looking for a solution where I can upload multiple MultipartFile along with input JSON body. I knew there are many related things available but none of the thing were I find my solution. which provoke me to ask a question to community.

My requirement is something likewise,

I have developed(In fact developing & am in last stage) open source library where schools/colleges/universities would willingly upload study materials for students based upon subject wise. Along with material User(authenticated) must has to provide few text info like, title, description, remarks.

so My expected entity is something likewise,

@Entity
class Material {
private MultipartFile[] content;
private String title;
private String description;
private String remarks;
}

I would like to use spring MVC feature something likewise where am getting failed & need your help here,

public String uploadMaterial(@RequestBody Material material) {
... business logic ...
return "successfully material saved.";
}

Any help will be really Appreciate!!

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55

3 Answers3

4
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public @ResponseBody Material createMaterial(@RequestPart("addtionalData") String addtionalData, @RequestPart("fileList") List<MultipartFile> fileList) throws IOException {

        Material material = new ObjectMapper().readValue(addtionalData, Material.class);
        material.setContent(fileList);
// do what ever you want to do
}
Muhammad Waqas
  • 367
  • 3
  • 13
1

I had a similar requirement for one of my projects and i used below technique to upload the images

public Job uploadImageForAJo(@PathVariable Long jobId, @RequestParam("file") MultipartFile[] files)

and here if you want you can pass a json payload with your other required information as the @RequestBody

you can have a look at below question to find how to test your api with postman

Tool for sending multipart/form-data request

keth
  • 793
  • 2
  • 11
  • 36
  • this won't work in my case. because I neither pass title, desc in form of @ Pathvariable, nor form-data. As I said, my first choice would be @ RequestBody only. Thanks for your suggestion & highlight this point. – Vishal Gajera Jan 31 '19 at 05:59
  • @vishalgajera may be you can convert images to base64 images and include as a json property – keth Jan 31 '19 at 06:02
0

@CrossOrigin(origins = "*") @PostMapping("/uploadDocuments") public Object uploadFilesOnAwsS3( @RequestHeader(name = "docId") String docId, @RequestHeader(name = "referenceId") String referenceId, @RequestHeader(name = "docTypeId") String docTypeId, @RequestParam(value = "data") Object data, @RequestParam(value = "files", required = true) MultipartFile[] files) throws IOException, MsuException { }