How to resolve FileSizeLimitExceededException error?
Asked Answered
H

5

5

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 !

Hubby answered 23/3, 2019 at 8:43 Comment(3)
The property is named spring.http.multipart.max-file-size, not Spring.http.multipart.max-file-size.Shults
Possible duplicate of How to set the max size of upload fileClastic
please try to clean the project and make sure you have not misspelled itTalkingto
M
8

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
Martel answered 1/1, 2020 at 16:6 Comment(1)
the question is about how to handle the FileSizeLimitExceededException exception, not how to set the max file sizeCrossopterygian
A
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.

Alphanumeric answered 23/3, 2019 at 9:16 Comment(3)
Thanks for your comment, this typo is only present over copy past of Stackoverflow page. in my application it could be small case only(ie., spring)Hubby
though this is not handling the FileSizeLimitExceededException, just setting the limit.Crossopterygian
how to handle the exception and show to user that messageEaring
F
2

Try using the below code:

spring:
  profiles: development
  servlet:
    multipart:
      enabled: true
      max-file-size: 10MB
      max-request-size: 10MB
Ferrol answered 5/2, 2020 at 8:42 Comment(0)
G
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;
}
Gamali answered 23/3, 2019 at 11:18 Comment(0)
D
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
Denysedenzil answered 20/9, 2022 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.