简体   繁体   English

FileUploadBase在上传小文件时找不到任何多部分

[英]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配置多部分。这会导致底层的HttpServletRequestcommons-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.

相关问题 Android HttpUrlConnection上传文件/多部分 - Android HttpUrlConnection uploading files/multipart 作为分段文件上传时是否有最大文件大小 - Is there a max file size when uploading as a Multipart File 用于上传文件的Java HTTP客户端(multipart / form-data) - Java HTTP Client for Uploading Files (multipart/form-data) 使用现有的多部分发布项目在Android中上载多个文件 - Uploading Multiple Files in Android using existing multipart post project 文件上传“multipart / form”异常org.apache.commons.fileupload.FileUploadBase $ InvalidContentTypeException - file upload “multipart/form” Exception org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException Undertow 上传多部分文件超过设置值时抛出 RuntimeException - Undertow throws RuntimeException when uploading multipart file exceeding the setting value 上载分段文件和连接中断时,Jetty占用100%CPU - Jetty takes 100% CPU when uploading multipart file and connection breaks 文件上传中断时“无法解析多部分 servlet 请求” - "Failed to parse multipart servlet request" when file uploading interrupted 将表单数据上传到远程Tomcat服务器时,multipart为null - Multipart is null when uploading formdata to remote Tomcat server 在短时间内将大量文件上传到 GCP 存储桶时遇到瓶颈 - Bottleneck while uploading lots of files to GCP bucket in a small time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM