11

I'm trying to make a simple upload app with springboot and it works fine until i try to upload 10Mb+ files, i receive this message on my screen:

There was an unexpected error (type=Internal Server Error, status=500).
Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (14326061) exceeds the configured maximum (10485760)

I've done some research, and nothing have worked until now. I'll leave here below the things i've tried so far.

put this code(In various ways) in my "application.yml"

multipart: 
 maxFileSize: 51200KB
 maxRequestFile: 51200KB  

I've also tried this in my principal class:

    @Bean
public TomcatEmbeddedServletContainerFactory containerFactory() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
     factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
         ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
        }
     });
     return factory;
}

And some strange thing. If i enter in my tomcat web.xml, the multipart-config is:

<multipart-config>
      <!-- 50MB max -->
      <max-file-size>52428800</max-file-size>
      <max-request-size>52428800</max-request-size>
      <file-size-threshold>0</file-size-threshold>
    </multipart-config>

So where the hell this "...configured maximum (10485760)" is coming from ? (Sidenote: I'm using netbeans 8.1 and springboot 1.5).

Thx guys.(And sorry for the english s2)

Since asked, this is my application.yml

 server:
      port: 9999
      context-path: /client
    logging:
      level:
        org.springframework.security: DEBUG
    endpoints:
      trace:
        sensitive: false

    spring:
        thymeleaf:
            cache: false
        multipart: 
          maxFileSize: 51200KB
          maxRequestFile: 51200KB  

    #################################################################################

    security:
      basic:
        enabled: false
      oauth2:
        client:
          client-id: acme2
          client-secret: acmesecret2
          access-token-uri: http://localhost:8080/oauth/token
          user-authorization-uri: http://localhost:8080/oauth/authorize
        resource:
          user-info-uri: http://localhost:8080/me
    #    
Estudeiro
  • 225
  • 2
  • 4
  • 15

6 Answers6

14
spring:
  http:
    multipart:
      enabled: true
      max-file-size: 50MB
      max-request-size: 50MB

or

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

Reference here

Hope it will works

Rahul Mahadik
  • 10,549
  • 5
  • 39
  • 47
  • Thx, it worked (: But how did you knew this,because it's not in the documentation, neither in the other solutions to this problem. And why i had to enable it and other people didn't have too ? – Estudeiro Mar 15 '18 at 17:20
  • @Estudeiro Welcome dude. :) – Rahul Mahadik Mar 15 '18 at 17:22
  • 1
    @Estudeiro I'm working on Grails, which have also same configuration file. and i seen your exception `org.apache.tomcat.util.http.fileupload.FileUploadBase` so – Rahul Mahadik Mar 15 '18 at 17:24
  • Since this is the accepted answer, I'll comment here on the latest syntax for Spring 2.6.1: (Note the `http` -> `servlet`): ``` spring.servlet.multipart.max-file-size=50MB spring.servlet.multipart.max-request-size=50MB ``` As mentioned in the answer, for the latest version, it's best to refer to the [docs](https://spring.io/guides/gs/uploading-files/) – JCash Jan 02 '22 at 08:52
3

For SpringBoot 1.5.7 till 2.1.2 the property need to set in application.properties file are:

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

Also make sure you have application.properties file in "resources" folder.

Atul
  • 2,480
  • 22
  • 31
3

Following are the ways based on version,

1'st :

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

2'nd :

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

3'rd :

multipart.enabled=true
multipart.max-file-size=100MB
multipart.max-request-size=100MB
Dhwanil Patel
  • 1,905
  • 1
  • 14
  • 23
1
spring:
  servlet: 
    multipart: 
       enabled: true 
       file-size-threshold: 200KB   
       max-file-size:       500MB 
       max-request-size:    500MB

ASH achraf
  • 11
  • 1
  • 6
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Nov 25 '20 at 09:10
1

In SpringBoot 2.6.3 setting "spring.http.multipart.max-file-size" did not work. Following did work for me:

spring:
  servlet:
    multipart:
      max-file-size: 50MB
      max-request-size: 50MB
dcnis
  • 61
  • 4
0

For configuring CommonsMultipartResolver Define a bean with bean name as MultipartFilter.DEFAULT_MULTIPART_RESOLVER_BEAN_NAME As the default spring boot's default MultipartFilter looks for resolver with default bean name.

@Bean(name = MultipartFilter.DEFAULT_MULTIPART_RESOLVER_BEAN_NAME)
protected MultipartResolver getMultipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(20971520);
    multipartResolver.setMaxInMemorySize(20971520);
    return multipartResolver;
}