Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion regarding spring.http.multipart.max-file-size vs spring.servlet.multipart.max-file-size

I have spent days wasted getting Spring Boot Upload file to work but, as is always with Spring you have no clue how the magic works and even after years of working with this framework - you have to google tonnes of times to unravel what has gone wrong and solve things like as if you are going thru a maze,it's a maintainability nightmare.

Using Spring Boot 2.2.0.M3 for file uploads what is the difference between the 2 pair's of settings ? Which is right ?

spring.http.multipart.max-file-size=-1
spring.http.multipart.max-request-size=-1

Is the above "http" used with Spring REST controller methods namely like this ... @GetMapping("/files/{filename:.+}") @ResponseBody public ModelAndView yourMethod(.....) Or is this not needed at all and is a complete red-herring and it is the setting below that does all the work for files bigger than the default of 1MB for both REST http or Servlet requests.

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1

Exception on uploading

Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.

like image 939
user1561783 Avatar asked Dec 22 '22 23:12

user1561783


1 Answers

They had changed the property names in different versions.

Spring Boot 1.3.x and earlier

multipart.max-file-size
multipart.max-request-size

After Spring Boot 1.3.x:

spring.http.multipart.max-file-size=-1
spring.http.multipart.max-request-size=-1

After Spring Boot 2.0:

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1

max-file-size Vs max-request-size

spring.servlet.multipart.max-file-size = 2MB

Max size per file the upload supports is 2MB;

also supports the MB or KB suffixes; by default 1MB


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

max size of the whole request is 10MB;

also supports the MB or KB suffixes

For unlimited upload file size, It seems setting -1 will make it for infinite file size.


UPDATE: You don't need to specify any spring.** property at Controller Level (expect headers Content-Type in some case). You can set these properties in appilcation.properties file as below.

# MULTIPART (MultipartProperties)
spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0B # Threshold after which files are written to disk.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
spring.servlet.multipart.max-file-size=1MB # Max file size.
spring.servlet.multipart.max-request-size=10MB # Max request size.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.
like image 161
Romil Patel Avatar answered Jun 05 '23 19:06

Romil Patel