SpringBoot's @MultipartConfig maxFileSize not taking effect

后端 未结 7 1509
野性不改
野性不改 2021-02-02 07:52

I have a controller with a MultipartConfig annotation (a snippet of which is show below):

@RestController
@RequestMapping(\"packages\")
@MultipartCo         


        
相关标签:
7条回答
  • 2021-02-02 08:26

    With spring-boot 1.5.3 you should use the following code in application.yml

    spring:
     http:
      multipart:
       max-file-size: 100MB
       max-request-size: 100MB
    

    Make sure to use spaces and not tab in your yaml file.

    0 讨论(0)
  • 2021-02-02 08:28

    With Spring Boot 2.0, you should use this in your application.yml

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

    From documentation:

    Spring Boot embraces the Servlet 3 javax.servlet.http.Part API to support uploading files. By default, Spring Boot configures Spring MVC with a maximum size of 1MB per file and a maximum of 10MB of file data in a single request. You may override these values, the location to which intermediate data is stored (for example, to the /tmp directory), and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. For example, if you want to specify that files be unlimited, set the spring.servlet.multipart.max-file-size property to -1.

    Extracted from Appendix A of documentation

    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.

    0 讨论(0)
  • 2021-02-02 08:32

    If you just want to control the multipart properties, you can use multipart.max-file-size and multipart.max-request-size properties. For example, you could raise the max size to 100Mb by adding following piece of configurations in your application.properties file:

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

    Values can use the suffixed MB or KB to indicate a Megabyte or Kilobyte size.

    Under the hood, Spring Boot will create a MultipartConfigElement based on MultipartProperties and that MultipartConfigElement will be used in Servlet registration, as stated in Spring MVC documentation. You can take a look at MultipartAutoConfiguration and DispatcherServletConfiguration and Checkout Spring Boot documentation for more information.

    0 讨论(0)
  • 2021-02-02 08:38

    For Spring Boot v2+ add the following to the application.properties:

    spring.servlet.multipart.max-file-size=10MB
    spring.servlet.multipart.max-request-size=40MB
    
    0 讨论(0)
  • 2021-02-02 08:39

    On deployment, where the values are inserted via jenkins script, no "-" is allowed. Use:

    env:
      - name: SPRING_SERVLET_MULTIPART_MAXREQUESTSIZE
        value: "10MB"
      - name: SPRING_SERVLE
        value: "10MB"
    
    0 讨论(0)
  • 2021-02-02 08:41

    For setting custom limits for multipart uploads, the below properties are to be used(for a sample size of 30MB):

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

    In our company's projects, I found that one of them was on Spring Boot version 1.3.5, so for versions < 1.4 you should use

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

    From the docs(v1.4.0):

    Spring Boot embraces the Servlet 3 javax.servlet.http.Part API to support uploading files. By default Spring Boot configures Spring MVC with a maximum file of 1MB per file and a maximum of 10MB of file data in a single request. You may override these values, as well as the location to which intermediate data is stored (e.g., to the /tmp directory) and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. If you want to specify that files be unlimited, for example, set the spring.http.multipart.max-file-size property to -1.

    The multipart support is helpful when you want to receive multipart encoded file data as a @RequestParam-annotated parameter of type MultipartFile in a Spring MVC controller handler method.

    Same docs for version 1.3.8:

    If you want to specify that files be unlimited, for example, set the multipart.maxFileSize property to -1.

    0 讨论(0)
提交回复
热议问题