【问题标题】:Not able to upload multiple files using InputStream in rest api无法在 rest api 中使用 InputStream 上传多个文件
【发布时间】:2020-05-22 13:28:20
【问题描述】:

我想使用spring在rest 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】:

    我已经用我的休息电话来上传多个文件,例如(未尝试使用 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();
    }
    

    在这里您将获得您正在寻找的输入流列表。

    【讨论】:

      猜你喜欢
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      相关资源
      最近更新 更多