0

I want to upload multiple files in the rest api using spring. I am able to upload single file using below code

    @Path("/line-item/cancel")
    @Produces({"application/xml", "application/json"})
    @Consumes({MediaType.MULTIPART_FORM_DATA})
    @ApiOperation(value = "Api to cancel PO Line Items", response = POLineItemCancellationResponse.class)
    POLineItemCancellationResponse cancelPoLineItems(@Multipart(value = "data") String poLineItemCancellationRequestEntry, @Multipart(value="file") InputStream inputStream);

But if I try to input @Multipart(value="file") InputStream[] inputStream then I am getting null object in input stream.

I have tried using MultiPartFile also

POLineItemCancellationResponse cancelPoLineItem(@RequestPart(value="file") MultipartFile[] files);

But I am getting the below error:

No message body reader has been found for class [Lorg.springframework.web.multipart.MultipartFile;

Aneesh Goel
  • 47
  • 2
  • 6

1 Answers1

0

I have used for my rest calls to upload multiple files like(not tried with srping)

@FormDataParam("file") FormDataContentDisposition fileDisposition,
            @FormDataParam("file") List<FormDataBodyPart> bodyParts

And this is swagger defination

@ApiImplicitParam(name = "file", value = "One or more files", allowMultiple = true, required = true, dataType = "file", paramType = "form")



for (int i = 0; i < bodyParts.size(); i++) {
            // * Casting FormDataBodyPart to BodyPartEntity, which can give us
            // * InputStream for uploaded file
            BodyPartEntity bodyPartEntity = (BodyPartEntity) bodyParts.get(i)
                    .getEntity();
            fileDisposition = bodyParts.get(i).getFormDataContentDisposition();
            try { 
                  String fileName = URLEncoder.encode(
                FilenameUtils.getName(fileDisposition.getFileName()), "UTF-8");
        String documentFormat = fileName
                .substring(fileName.lastIndexOf(".") + 1);
        InputStream file = bodyPartEntity.getInputStream();
}

Here you will get the list of input streams you are looking for.

Seshidhar G
  • 265
  • 1
  • 9