Max Upload File Size in Spring Framework

 Spring framework comes preconfigured with default upload file size and max request size. You can modify the sizes with application settings.

Settings

  • max-file-size specifies the maximum size permitted for uploaded files. The default is 1MB
  • max-request-size specifies the maximum size allowed for multipart/form-data requests. The default is 10MB.

Error exceeding max file size

Java with Spring 6 will throw a MaxUploadSizeExceededException exception when the max size is exceeded.

threw exception [Request processing failed: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded] with root cause

org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException: The field image exceeds its maximum permitted size of 1048576 bytes.
	at org.apache.tomcat.util.http.fileupload.impl.FileItemStreamImpl$1.raiseError(FileItemStreamImpl.java:117) ~[tomcat-embed-core-10.1.1.jar:10.1.1]
	at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76) ~[tomcat-embed-core-10.1.1.jar:10.1.1]
	at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135) ~[tomcat-embed-core-10.1.1.jar:10.1.1]
	at java.base/java.io.FilterInputStream.read(FilterInputStream.java:106) ~[na:na]

Solution

Add the following settings to the application.properties.

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

Or if you’re using yaml in the application.yml.

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

Comments

  1. you made a mistake: max-request-size twice
    right way:

    spring.servlet.multipart.max-file-size=80MB
    spring.servlet.multipart.max-request-size=160MB

    ReplyDelete

Post a Comment

Popular posts from this blog

Use Java Enums with JPA

Spring Security part 3 - OidcUser