【问题标题】:Spring upload file size limit errorSpring上传文件大小限制错误
【发布时间】:2018-12-26 21:02:42
【问题描述】:

我正在使用 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 upload file size limitI am trying to set maxFileSize but it is not honoredorg.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededExceptionMax Limit of MultipartFile in spring boot

我使用的是 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);
  }

}

以下是我尝试过的所有 application.properties 配置:

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 捆绑在应用程序中。

【问题讨论】:

  • 您使用过任何网关吗?或任何其他服务将请求转发给您的实际请求。如果是,请从入口点到真正的服务一路添加spring.servlet.multipart配置。
  • @TroyYoung - 对我来说就是这样。谢谢!

标签: java spring spring-boot tomcat spring-restcontroller


【解决方案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

【讨论】:

  • 能否解释一下,为什么提到spring.httpspring.servlet?你都设置了吗?
  • 正如答案中提到的,对于 spring 版本 2.0.1,我们需要使用 spring.servlet,对于 spring 版本 2.x,我们需要使用 spring.http。
  • 哦,我没有注意到关于版本号的备注。感谢您的澄清!我在这方面编辑了你的帖子。希望你没问题。
  • 谁能帮我理解为什么我们需要设置。 'spring.servlet.multipart.enabled=true' 如果默认为真。我遇到了一个最近的项目,其中 multipart 无法正常工作,我们必须将其设置为 enabled = true 我们仍然很困惑为什么......github.com/spring-projects/spring-boot/blob/…
  • spring.servlet.multipart.enabled=true 让我开心!
【解决方案2】:

根据最新的Spring Boot Common properties 下面应该可以工作

MULTIPART(多部分属性)

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

【讨论】:

    【解决方案3】:

    取决于 Spring boot 版本,例如 1.X,在您的 application.properties 上,设置文件大小限制:

    spring.http.multipart.max-file-size=128KB
    spring.http.multipart.max-request-size=128KB
    

    Spring Boot 2.x / 以上:

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

    【讨论】:

      【解决方案4】:

      这就是我所拥有的,它非常适合我的后端。

      @PutMapping(value = USER_PROFILE_UPDATE_URL_MAPPING)
        public String updateProfile(
            @ModelAttribute(AUTHORIZED_USER_MODEL_KEY) User user,
            BindingResult bindingResult,
            @RequestParam(name = "file") MultipartFile multipartFile,
            Model model, @RequestParam Map<String, String> params) {
      
          Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
          User authorizedUser = (User) authentication.getPrincipal();
          //.... 
      
            return REDIRECT_TO_LOGIN;
          }
      

      在 bootstrap.yml 文件中,我会简单地有这样的东西...... 这将只允许 2 兆字节的最大文件大小。

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

      最后,在我的 HTML 文件中,我会有这样的东西......

      <form id="profileForm"
         th:action="@{/update}"
         th:object="${user}"
         method="post" enctype="multipart/form-data">
      
         <!-- This will modify the method to PUT to match backend -->
         <input type="hidden" name="_method" value="PUT">
      
         ...
      
         <div class="col-md-3">
            <div class="form-group row">
               <label for="file" class="custom-file">
                 <input name="file" type="file" id="file" aria-describedby="fileHelpId"  class="form-control-file btn btn-outline-info">
                </label>
                <small id="fileHelpId" class="form-text text-muted">Maximum size should be 2MB</small>
             </div>
         </div>
      

      这应该可以正常工作。我也在使用 Spring Boot 2.0.3

      【讨论】:

      • 与我的相比,您的有什么不同可能导致此错误吗?除了applications.yml/applications.properties文件。
      • 不,没有,但我知道 yml 文件与属性相比优先。我遇到了同样的问题,但是当我更改它时,它对我有用,所以我分享了。
      • hmmmm 我也改了,还是报错
      【解决方案5】:

      你试过了吗?

      spring.http.multipart.maxFileSize = 20MB
      spring.http.multipart.maxRequestSize = 20MB
      

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

      适用于我的 Spring Boot 1.5.x

      【讨论】:

      • 两种表示法配置相同的属性——感谢 Spring 的Relaxed Binding
      【解决方案6】:
      This worked for me.
      We can make changes to file size either in
      1. application.properties 2. TOMCAT configuration(server.xml)
      
      A. Ideally we should make use of application.properties 
      B. And for that first we need to disable in TOMCAT config file "server.xml" by setting maxSwallowSize=-1 .
      
          <Connector port="8080" protocol="HTTP/1.1"
            connectionTimeout="20000"
            redirectPort="8443"
            maxSwallowSize = "-1"/>
      
      C. Then set application.properties as
      
      spring.servlet.multipart.max-file-size=15MB
      spring.servlet.multipart.max-request-size=15MB
      spring.servlet.multipart.enabled=true
      

      【讨论】:

        猜你喜欢
        • 2017-03-09
        • 1970-01-01
        • 2019-04-10
        • 1970-01-01
        • 1970-01-01
        • 2011-01-29
        • 2011-01-13
        • 1970-01-01
        • 2022-10-26
        相关资源
        最近更新 更多