0

在此处输入图像描述我想在 spring boot 控制器中接收多行,尝试了不同的方法但无法做到。我正在用邮递员进行测试。

控制器

@PostMapping(URLConstant.URL_SC_ATTACHMENT_POST)
public ResponseEntity<ApiResponse> storeFile(@RequestParam("attachmentDto") List<BudgetSceAttachmentDto> attachmentDto) throws IOException {
    System.out.println(attachmentDto);
    return ResponseUtil.getResponse(HttpStatus.OK, MsgConstant.BUDGET_MSG_FILE_UPLOADED, null);

}

DTO

private Integer versionId;

private String fileName;

private String pathUploadedFile;

private String uploadedFileName;

private MultipartFile file;
4

1 回答 1

0

首先,请求的内容类型应该是multipart/form-data.

然后让我们简化这个问题 - 首先检查上传多个文件。

@PostMapping(URLConstant.URL_SC_ATTACHMENT_POST)
public ResponseEntity<ApiResponse> storeFile(@RequestParam MultipartFile[] files) throws IOException {
    Assert.isTrue(files.length == 2, "files length should be 2");
    System.out.println(files.length);
    return ResponseUtil.getResponse(HttpStatus.OK, MsgConstant.BUDGET_MSG_FILE_UPLOADED, null);
}

如果它运作良好,现在是时候再次带上 DTO。

@PostMapping(URLConstant.URL_SC_ATTACHMENT_POST)
public ResponseEntity<ApiResponse> storeFile(@ModelAttribute List<BudgetSceAttachmentDto> params) throws IOException {
    Assert.isTrue(params.length == 2, "files length should be 2");
    System.out.println(params.length);
    return ResponseUtil.getResponse(HttpStatus.OK, MsgConstant.BUDGET_MSG_FILE_UPLOADED, null);
}
于 2019-09-11T12:27:24.210 回答