使用Spring Boot和Swagger管理多个文件上传

时间:2019-01-14 10:44:29

标签: spring-boot swagger-ui

我正在尝试使用Spring Boot控制器的以下方法来管理多个文件上传:

    @RequestMapping(value = "/rest/multipleupload", method = RequestMethod.POST)
public ResponseEntity uploadFile(@RequestPart String metaData,
        @RequestPart(required = true) MultipartFile[] uploadfiles) {
    // Get file name
    String uploadedFileName = Arrays.stream(uploadfiles).map(x -> x.getOriginalFilename())
            .filter(x -> !StringUtils.isEmpty(x)).collect(Collectors.joining(" , "));

    if (StringUtils.isEmpty(uploadedFileName)) {
        return new ResponseEntity("please select a file!", HttpStatus.OK);
    }

    try {

        saveUploadedFiles(Arrays.asList(uploadfiles));

    } catch (IOException e) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    return new ResponseEntity("Successfully uploaded - " + uploadedFileName, HttpStatus.OK);
}

当我使用Swagger UI执行测试时,返回以下错误:

"Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided"

这是Swagger UI提供的提示:

curl -X POST "http://localhost:8080/rest/multipleupload" -H  "accept: */*" -H  "Content-Type: application/json" -d "metaData=dddd&uploadfiles=%5Bobject%20File%5D&uploadfiles=%5Bobject%20File%5D"

在类似的线程中,建议添加Apache的CommonsMultipartResolver:

@Bean(name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver() {
    return new CommonsMultipartResolver();
}

但是,它无助于解决问题。 我也尝试添加:

spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=2KB
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=215MB

但是它也没有帮助。有什么建议吗?

0 个答案:

没有答案