Estudeiro
Estudeiro

Reputation: 403

"the request was rejected because its size " Spring, tomcat

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
    #    

Upvotes: 17

Views: 49939

Answers (8)

megloff
megloff

Reputation: 1566

For me the following worked in application.properties of a Spring Boot 3 application (v3.0.5). This should be also the correct properties according to the following guide / reference of spring.io and as well here at the source code documentation and the related Spring Boot Multipart Auto Configuration.

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

I had NOT to add any special @Beanconfiguration. According to the Multipart Auto Configuration documentation:

Auto-configuration for multipart uploads. Adds a
StandardServletMultipartResolver if none is present, and adds a 
multipartConfigElement if none is otherwise defined. The 
ServletWebServerApplicationContext will associate the 
MultipartConfigElement bean to any Servlet beans. 

the following did NOT work (may be it worked in an earlier version of Spring boot).

#spring.http.multipart.enabled=true
#spring.http.multipart.max-file-size=128MB
#spring.http.multipart.max-request-size=128MB

Upvotes: 0

bkoz
bkoz

Reputation: 1

I also had this problem and I don't know why setting properties spring.http.multipart.max-file-size=20MB and spring.http.multipart.max-request-size=20MB in application.properties didn't work. To change max file size I've followed this guide https://www.baeldung.com/spring-maxuploadsizeexceeded

So I've added this to my principal class:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver
      = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(20000000);
    return multipartResolver;
}

and then, to handle MaxUploadSizeExceededException, I've copied this

@ControllerAdvice
public class FileUploadExceptionAdvice {
     
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView handleMaxSizeException(
      MaxUploadSizeExceededException exc, 
      HttpServletRequest request,
      HttpServletResponse response) {
 
        ModelAndView modelAndView = new ModelAndView("file");
        modelAndView.getModel().put("message", "File too large!");
        return modelAndView;
    }
}

and wrote this simple file.html template:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3 th:text="${message}"></h3>
</body>
</html>

After adding this code I've seen in logs that the MaxUploadSizeExceededException error was handled, but in browser I still got error. The solution was adding this to application.properties:

server.tomcat.max-swallow-size=60MB

like in this tutorial: https://www.youtube.com/watch?v=ZZMcg6LHC2k

Upvotes: 0

dcnis
dcnis

Reputation: 161

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

Upvotes: 1

Rahul Mahadik
Rahul Mahadik

Reputation: 12291

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

Upvotes: 22

ASH achraf
ASH achraf

Reputation: 21

spring:
  servlet: 
    multipart: 
       enabled: true 
       file-size-threshold: 200KB   
       max-file-size:       500MB 
       max-request-size:    500MB

Upvotes: 2

Dhwanil Patel
Dhwanil Patel

Reputation: 2573

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

Upvotes: 9

Yuvraj P
Yuvraj P

Reputation: 1

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;
}

Upvotes: 0

Atul
Atul

Reputation: 3377

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.

Upvotes: 3

Related Questions