2

I could able to use below code to upload a single photo, but I want to upload multiple photos with @FormDataParam

@POST
@Path("data/uploadPhoto")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadPhoto(@FormDataParam("data") InputStream photo) {

I tried to use @FormDataParam("file") List<InputStream> photos

but its not worked out, any suggestions?

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
Digital
  • 549
  • 1
  • 7
  • 26
  • Possible duplicate of [Selecting multiple files and uploading them using Jersey](https://stackoverflow.com/questions/25749472/selecting-multiple-files-and-uploading-them-using-jersey) – Dushyant Tankariya Jun 05 '19 at 10:01

2 Answers2

0

Since each part of the request must have a unique name, you can not use the same name file for every image. The request must use different names.

It followes that your method must have one @FormDataParam for each file in the request. All of these must have different names.

public Response uploadPhoto(@FormDataParam("data1") InputStream photo1,
  @FormDataParam("data2") InputStream photo2,
  @FormDataParam("data3") InputStream photo3) {
  • thanks for the response, here photos are dynamic and I don't know how many of the photos will come in the request, so wanted to handle in a generic way. – Digital Jun 08 '19 at 18:54
0

You can upload multiple Files by Using FormDataBodyPart Class which allows you to get the form data and you can grab the multiple images as InputStream from it.

I've posted a sample below,

 @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public void uploadMultiple(@FormDataParam("file") FormDataBodyPart body){
        for(BodyPart part : body.getParent().getBodyParts()){
            InputStream is = part.getEntityAs(InputStream.class);
            ContentDisposition meta = part.getContentDisposition();
            doUpload(is, meta);
        }
    }

You Question possible duplicate of Selecting multiple files and uploading them using Jersey

Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17