弹簧上传文件大小限制错误

我正在使用Spring Boot,我可以发送小于1MB的图像,但是当我使用大于1MB的大图像发出发布请求时,我收到以下错误:

Maximum upload size exceeded; nested exception is java.lang.IllegalStateException:org.apache.tomcat.util.
http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.

我一直在寻找很多地方,试图找到这个错误的答案。我已经研究了所有这些问题,并尝试实现它们的建议,但无济于事:Spring上传文件大小限制我正在尝试设置maxFileSize,但它没有得到尊重org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceedException,以及Spring boot中MultipartFile的最大限制

我正在使用Spring的2.0.3版本,这是我的帖子映射:

    @PostMapping("/post")
    public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
      String message = "";
      try {
        storageService.store(file);
        files.add(file.getOriginalFilename());

        message = "You successfully uploaded " + file.getOriginalFilename() + "!";
        return ResponseEntity.status(HttpStatus.OK).body(message);
      } catch (Exception e) {
      message = "FAIL to upload " + file.getOriginalFilename() + "!";
      return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(message);
  }

}

以下是我尝试过的所有应用程序.属性配置:

1

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1

2

spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB

3

spring.http.multipart.max-file-size=5MB
spring.http.multipart.max-request-size=5MB

4

multipart.max-file-size=5MB
multipart.max-request-size=5MB

5

server.tomcat.max-file-size=5000000

6

server.tomcat.max-http-post-size=5000000

7

spring.servlet.multipart.maxFileSize=-1
spring.servlet.multipart.maxRequestSize=-1

甚至尝试将其更改为 application.yml:

spring:
  servlet:
    multipart:
      max-file-size: 5MB
      max-request-size: 5MB

我还考虑在web.xml文件中更改Tomcat允许的请求大小,但我没有web.xml文件。我正在使用的Tomcat被捆绑到应用程序中。


答案 1

application.properties 中添加以下行,适用于 Spring Boot 版本 2.0.0.RELEASE 及更高版本 -

spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
spring.servlet.multipart.enabled=true

请注意,对于较低版本,即 1.5.9.RELEASE 及以下版本,过去的配置如下所示:

spring.http.multipart.max-file-size = 20MB
spring.http.multipart.max-request-size = 20MB

答案 2

根据最新的弹簧靴常见属性,下面的属性应该可以工作

多部分(多部分属性)

spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0 # Threshold after which files are written to disk. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
spring.servlet.multipart.max-file-size=1MB # Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.max-request-size=10MB # Max request size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.

或者,如果您只想控制多部分属性,那么和属性应该可以正常工作。multipart.max-file-sizemultipart.max-request-size

multipart.max-file-size=5MB
multipart.max-request-size=5MB

推荐