I am using Spring Boot 1.1.3 with the CommonsMultipartResolver
to allow uploading of multiple files at once.
I get this stacktrace when I try to upload a file bigger than 1 MB:
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException:
The field files[] exceeds its maximum permitted size of 1048576 bytes.
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:637)
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76)
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
I tried setting the max upload size like this:
public MultipartResolver multipartResolver()
{
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSize( 100 * MEGABYTE_IN_BYTES );
return resolver;
}
However, this does not work. I have found this Spring guide on upoading files and there they use MultipartConfigFactory
instead. However, I now need to use the MultipartFile
class instead of MultipartHttpServletRequest
in my controller.
With the MultipartHttpServletRequest
I could do getFileMap()
to get all the files, but there is no such method on MultipartFile
.
Any ideas on how to work with MultipartConfigFactory
and multiple files? I am using jquery-file-upload on the client if that would matter.
The right way to increase upload limit is to set propertymultipart.maxFileSize=10Mb
In your application.properties
file. You can read more on this topic here
https://stackoverflow.com/a/27062232/668417
and here
MultipartProperties.java
The question was asked with regard to Spring Boot 1.1.3. As time marches forward the accepted solutions no longer work with newer versions of Spring Boot. However the problem of increasing the maximum allowable upload size persists.
I am writing this as of Spring Boot 1.4.2, so this solution may not be correct in perpetuity.
From the spring guide to uploading files the new properties are thus:
spring.http.multipart.max-file-size=10Mb
spring.http.multipart.max-request-size=10Mb
for the size problem in a my project I use this properties in application.properties:
multipart.max-file-size=100Mb
multipart.max-request-size=100Mb
for the case of have multiple file I suggest of use a bean like this
@Data
class MultipartDTO{
private MultipartFile file1;
private MultipartFile file2;
.....
private MultipartFile filen;
//eventually other proerties
}
then of course in your controller you colud have a thing like this
@RequestMapping(value = "/multipartUrl", method = RequestMethod.POST, consumes = "multipart/form-data")
public ResponseEntity multipartCall(MultipartDTO multipartDTO){
....
}
I hope this can help you
Seems there is no need to use the MultipartFile
in the Controller, you can also use the MultipartHttpServletRequest
. This is the signature of my @RestController
annotated class:
@RequestMapping(value = "/{datasheetId}/addDoc", method = RequestMethod.POST)
@Secured(ROLE_USER)
public ResponseEntity<?> addDocumentToDatasheet( Principal principal,
@PathVariable("datasheetId") long datasheetId,
MultipartHttpServletRequest request ) throws IOException
{
Map<String, MultipartFile> fileMap = request.getFileMap();
// handle fileMap further...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With