[英]FileUploadBase does not find any multipart parts when uploading small files
I am using Spring's CommonsMultipartResolver
to process file uploads in a servlet 3 environment. 我使用Spring的
CommonsMultipartResolver
来处理servlet 3环境中的文件上传。
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. 我已将此跟踪到
apache.commons.fileupload.FileItemIteratorImpl
,其中findNextItem()
方法返回false,尽管帖子中有多个有效部分。 This results in no MultipartFile
object being available to my controller method. 这导致我的控制器方法没有可用的
MultipartFile
对象。
When I look in the debugger at the HttpServletRequest
I see the correct number of parts ( getParts()
returns the correct number of parts). 当我在
HttpServletRequest
查看调试器时,我看到正确的部件数量( getParts()
返回正确的部件数量)。
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
). 我可以使用
HttpServletRequest
,但对于大文件(> 1MB),会抛出一个关于最大文件大小的异常(我已经为CommonsMultipartResolver
成功配置但显然没有交叉到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. 我看了一下尝试配置Servlet 3的最大文件大小,但我不想在我的应用程序中添加几个新类来设置该大小。
Is there a way to upload smaller files using Servlet 3 and commons-fileupload
? 有没有办法使用Servlet 3和
commons-fileupload
上传较小的文件?
More 更多
I have commons-fileupload
configured as maxUploadSizePerFile
=100MB. 我有
commons-fileupload
配置为maxUploadSizePerFile
= 100MB。
The following behavior results: 以下行为导致:
If the uploaded file is > 10MB, then commons-fileupload
processes it and everything is fine. 如果上传的文件大于10MB,那么
commons-fileupload
处理它,一切都很好。
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. 如果上传的文件介于~3.8 MB和10 MB之间,则在
commons-fileupload
有机会解析导致浏览器中的连接重置消息的请求之前,输入流和底层连接都将关闭。
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. 对于介于1 MB和~3.8 MB之间的上载文件,输入流将关闭,但不会关闭底层连接,从而允许我的错误页面显示错误。
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. 最后,如果上传的文件小于1 MB,则底层servlet 3实现在调用
commons-fileupload
之前成功处理部件,使commons-fileupload
认为请求中没有部件。
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. 这种行为是因为使用Spring Boot(以前我不知道)会自动为Servlet 3配置多部分。这会导致底层的
HttpServletRequest
在commons-fileupload
有机会之前处理文件上传。
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): 将CommonsMultipartResolver配置为bean(很可能你已经这样做了):
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;
}
Disable spring servlet multipart handling in your application.properties file 在application.properties文件中禁用spring servlet multipart处理
spring.servlet.multipart.enabled=false
Then rejoice with great enthusiasm. 然后以极大的热情欢喜快乐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.