11

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.

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
user1561783
  • 483
  • 1
  • 4
  • 14

1 Answers1

15

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.
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
  • So the property names starting with spring.http.** etc. etc. have no effect and do not play a role in pure REST like requests that are annotated with the following as mentioned in my question above.
    Namely ...
    @GetMapping("/files/{filename:.+}")
    @ResponseBody
    Correct ?
    – user1561783 May 20 '19 at 17:42
  • Yes, GetMapping(...) Is used to map the given URL with GET method, if you are using RestController than you don't even need to use @ResponseBody – Romil Patel May 20 '19 at 17:51
  • Apologies. What I meant was the spring.http.** property setting is not needed for those @GetMapping(.....) REST-like annotations. In fact the spring.http.** is not needed AT ALL. Correct? the spring.servlet.** property setting is all that is needed for any file upload situation. Correct? – user1561783 May 20 '19 at 18:12
  • Thanks for answering, btw. – user1561783 May 20 '19 at 18:14