I am using Spring's CommonsMultipartResolver
to process file uploads in a servlet 3 environment.
If the uploaded file is large, everything works fine.
If the uploaded file is smaller, the resolver fails to discover any parts (with no exception thrown).
I have tracked this down to apache.commons.fileupload.FileItemIteratorImpl
for which the findNextItem()
method returns false, despite there being multiple valid parts in the post. This results in no MultipartFile
object being available to my controller method.
When I look in the debugger at the HttpServletRequest
I see the correct number of parts ( getParts()
returns the correct number of parts).
I could just use the HttpServletRequest
, except that for large files (>1MB), an exception is thrown about the maximum file size (which I have successfully configured for the CommonsMultipartResolver
but evidently does not cross over to HttpServletRequest
).
I looked at attempting to configure the Servlet 3 maximum file size, but I don't want to add several new classes to my application only to set that size.
Is there a way to upload smaller files using Servlet 3 and commons-fileupload
?
More
I have commons-fileupload
configured as maxUploadSizePerFile
=100MB.
The following behavior results:
If the uploaded file is > 10MB, then commons-fileupload
processes it and everything is fine.
If the uploaded file is between ~3.8 MB and 10 MB, both the input stream and the underlying connection are closed before commons-fileupload
has a chance to parse the request leading to a connection reset message in the browser.
For uploaded files between 1 MB and ~3.8 MB, the input stream is closed, but not the underlying connection, allowing my error page to display the error.
And finally, if the uploaded file is less than 1 MB, the underlying servlet 3 implementation successfully handles the parts before commons-fileupload
gets invoked, leaving commons-fileupload
believing that there were no parts in the request.
This behavior results from using Spring Boot which (previously unbeknownst to me) automatically configures multi-part for Servlet 3. This causes the underlying HttpServletRequest
to process the file upload before commons-fileupload
has a chance at it.
The only thing that I find curious at this point, is why it does not fail if the uploaded file is large enough.
Yeah, this is totally caused by spring boot jumping in the way and trying to help you out, which in this case causes problems. Here are the steps to fix it:
Configure CommonsMultipartResolver as a bean (more than likely you already did this):
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;
}
Disable spring servlet multipart handling in your application.properties file
spring.servlet.multipart.enabled=false
Then rejoice with great enthusiasm.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.