Journeycorner
Journeycorner

Reputation: 2542

Howto upload multiple files with Spring Boot and test it with cURL

I have implemented a Controller to upload multiple files:

public class Image implements Serializable {
    private MultipartFile file;
    private Ingeger imageNumber;
    ...
}

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void handleFileUpload(@RequestBody Set<Image> images) {
   ...
}

I correctly checked the code using only one MultipartFile directly in the upload method using this command:

curl http://localhost:8080/upload -X POST -F '[email protected];type=image/jpg' -H "Content-Type: multipart/form-data"

I need to extend it in three ways but don't know the correct syntax:

  1. POST a collection of JSON items
  2. Add the field "imageNumber" for each item
  3. Most tricky part: add a file nested to each item

Upvotes: 1

Views: 10958

Answers (1)

Journeycorner
Journeycorner

Reputation: 2542

I solved it using an Array instead of a Set with nested files.

Java:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void handleFileUpload(@RequestParam MultipartFile[] images, @RequestParam Integer[] numbers) {
   ...
}

cURL:

curl http://localhost:8080/upload -X POST \
-F '[email protected];type=image/jpg' \
-F 'numbers=1' \
-F '[email protected];type=image/jpg' \
-F 'numbers=2' \
-F '[email protected];type=image/jpg' \
-F 'numbers=3'

Upvotes: 8

Related Questions