无法在rest api中使用InputStream上传多个文件

问题描述 投票:0回答:1

我想使用spring在其余api中上传多个文件。我可以使用以下代码上传单个文件

    @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);

但是,如果我尝试输入@Multipart(value="file") InputStream[] inputStream,则输入流中将出现空对象。

我也尝试过使用MultiPartFile

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

但是我收到以下错误:

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

java spring rest multipart
1个回答
0
投票

我已经用我的休息电话上传了多个文件,例如(未尝试过srping)

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

这是昂首阔步的定义

@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();
}

这里您将获得所需的输入流列表。

© www.soinside.com 2019 - 2024. All rights reserved.