Post ajaxForm from Struts2 file upload utility not

2019-04-17 09:27发布

问题:

Background: I am looking for a tool to upload big file asynchronously using ajax + Struts2, I was able to do the same thing using servlet but when I modify the logic to call struts action. I have noticed that when I try to upload a huge file using struts2 action, it doesn't get called from jquery ajaxForm(options);

I have used the sample code specified on the below link this works perfectly fine. http://www.simplecodestuffs.com/file-upload-with-progress-bar-using-jquery-in-servlet/

Can anyone tell if below jquery function call is correct for upload functionality. $("#uploadtest").ajaxForm(options);

I tried but it is not working as expected in one particular browser when huge data is uploaded. (That is, client ajax call occurs, however, the corresponding struts2 action is not getting called in the backend, logs are not generated on the server side). I am not able to understand why struts2 action is not getting called when jquery ajaxform to upload huge file (multipart upload functionality).

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

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

Similar question is asked here ..FormData in IE8/9

回答1:

The problem with uploading big files to the Struts2 action is that request may not comply the limits used by default with Struts2. In configuration settings the value is set to 2097152. You can also set the limits per action. More about it you can find in 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

The next section from this docs page is File Size Limits where you have noticed about limitations of the file size used by the underline frameworks (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>

If the file size exceeds the above configuration settings, the pseudo progress bar stops as soon as it returns a response. It could be 1% or 100% it depends on thresh speed and a file size. But on the server side you might see an exception

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

and the following warnings. You might adjust the file size limitations with the framework if it doesn't exceed the framework's limitations itself.