Post 来自 Struts 的 ajaxForm 2 文件上传实用程序在 IE 中不工作

Post ajaxForm from Struts 2 file upload utility not working in IE

背景: 我正在寻找一种使用 Ajax + Struts 2 异步上传大文件的工具,我可以使用 servlet 做同样的事情,但是当我修改逻辑以调用 Struts 操作时。我注意到当我尝试使用 Struts 2 操作上传一个大文件时,它不会从 jQuery ajaxForm(options);

中调用

我使用了下面指定的示例代码 link 这工作得很好。 http://www.simplecodestuffs.com/file-upload-with-progress-bar-using-jquery-in-servlet/

任何人都可以判断以下 jQuery 函数调用对于上传功能是否正确。

$("#uploadtest").ajaxForm(options);  

我试过了,但当上传大量数据时,它在某一特定浏览器中无法正常工作。 (也就是说,客户端ajax调用发生,但是,相应的Struts 2动作在后端没有被调用,服务器端没有生成日志)。

我无法理解为什么 Struts 2 操作在 jQuery ajaxform 上传大文件(分段上传功能)时没有被调用。

jQuery:

$("#uploadtest").ajaxForm(options);

JSP 片段:

<s:form id="uploadtest" name="uploadform" action="aStrutsAction" method="post" enctype="multipart/form-data">

问了类似的问题here

将大文件上传到 Struts2 操作的问题是请求可能不符合 Struts2 默认使用的限制。在配置设置中,该值设置为 2097152。您还可以设置每个操作的限制。您可以在 Struts2 File Upload - Advanced Configuration:

中找到更多相关信息

The Struts 2 default.properties file defines several settings that affect the behavior of file uploading. You may find in necessary to change these values. The names and default values are:

struts.multipart.parser=jakarta
struts.multipart.saveDir=
struts.multipart.maxSize=2097152

此文档页面的下一部分是 File Size Limits 您已经注意到下划线框架(struts2、commons-fileupload)使用的文件大小的限制:

There are two separate file size limits. First is struts.multipart.maxSize which comes from the Struts 2 default.properties file. This setting exists for security reasons to prohibit a malicious user from uploading extremely large files to file up your servers disk space. This setting defaults to approximately 2 megabytes and should be adjusted to the maximum size file (2 gigs max) that your will need the framework to receive. If you are uploading more than one file on a form the struts.multipart.maxSize applies to the combined total, not the individual file sizes. The other setting, maximumSize, is an interceptor setting that is used to ensure a particular Action does not receive a file that is too large. Notice the locations of both settings in the following example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.multipart.maxSize" value="1000000" />
     
    <action name="doUpload" class="com.example.UploadAction">
        <interceptor-ref name="basicStack"/>
        <interceptor-ref name="fileUpload">
            <param name="maximumSize">500000</param>
        </interceptor-ref> 
        <interceptor-ref name="validation"/>
        <interceptor-ref name="workflow"/>
 
        <result name="success">good_result.jsp</result>
    </action>
</struts>

如果文件大小超过上述配置设置,伪进度条会在returns响应后立即停止。它可能是 1% 或 100%,这取决于脱粒速度和文件大小。但是在服务器端你可能会看到一个异常

org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (xxx) exceeds the configured maximum (yyy)

以及以下警告。如果不超过框架本身的限制,您可以使用框架调整文件大小限制。