4

I'm getting error while uploading excel file size is more the 1MB.

[org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request;
 nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException:
 The field files exceeds its maximum permitted size of 1048576 bytes.]

I tried to fix it by applying the following config changes but none of them help me.

Try with configuration in application.yml file :

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

And also I've tried the below annotation:

@MultipartConfig(fileSizeThreshold=1024*1024*10,maxFileSize=1024*1024*10,maxRequestSize=1024*1024*10)

And last I made this change:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" maxSwallowSize="-1"   />

But nothing does work !

Abdelghani Roussi
  • 2,707
  • 2
  • 21
  • 39
user3091530
  • 620
  • 2
  • 8
  • 22
  • The property is named `spring.http.multipart.max-file-size`, not `Spring.http.multipart.max-file-size`. – JB Nizet Mar 23 '19 at 08:57
  • Possible duplicate of [How to set the max size of upload file](https://stackoverflow.com/questions/37540028/how-to-set-the-max-size-of-upload-file) – Simulant Mar 23 '19 at 08:59
  • please try to clean the project and make sure you have not misspelled it – Romil Patel Mar 23 '19 at 09:16

5 Answers5

7

I tried Alien's solution but it gave deprecated error, hence I want to share new way of solution

spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.max-file-size=10MB
Semir Umut Kurt
  • 485
  • 5
  • 12
  • the question is about how to handle the FileSizeLimitExceededException exception, not how to set the max file size – Pablo Pazos Jul 24 '22 at 05:20
2

There is a typo in your property S in Spring is uppercase instead of lowercase.

With spring-boot 1.5.2 you can use the following property 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.

Alien
  • 15,141
  • 6
  • 37
  • 57
2

Try using the below code:

spring:
  profiles: development
  servlet:
    multipart:
      enabled: true
      max-file-size: 10MB
      max-request-size: 10MB
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

Include below code in your SpringBootWebApplication class(Main):

For Java 8:

@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
        if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
            //-1 for unlimited
            ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
        }
    });
    return tomcat;
}

for Java 7:

@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer()  {
        @Override
        public void customize(Connector connector) {
            if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
                //-1 for unlimited
                ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
            }
        }
    });
    return tomcat;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Akki
  • 73
  • 1
  • 11
0

For Spring boot 2.x and above its

Application properties

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

YAML

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