本文介绍了从 Struts 2 文件上传实用程序发布 ajaxForm 在 IE 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Background:I am looking for a tool to upload big file asynchronously using Ajax + Struts 2, 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 Struts 2 action, it doesn't get called from jQuery ajaxForm(options);

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

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/

谁能判断下面的 jQuery 函数调用对于上传功能是否正确.

Can anyone tell if below jQuery function call is correct for upload functionality.

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

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

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 Struts 2 action is not getting called in the backend, logs are not generated on the server side).

我不明白为什么在 jQuery ajaxform 上传大文件(分段上传功能)时没有调用 Struts 2 操作.

I am not able to understand why Struts 2 action is not getting called when jQuery ajaxform to upload huge file (multipart upload functionality).

jQuery:

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

JSP 代码段:

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

此处提出类似问题.

推荐答案

将大文件上传到 Struts2 操作的问题是请求可能不符合 Struts2 默认使用的限制.在配置设置中,该值设置为 2097152.您还可以设置每个操作的限制.有关更多信息,您可以在 Struts2 文件上传 - 高级配置:

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:

Struts 2 default.properties 文件定义了几个影响文件上传行为的设置.您可能会发现有必要更改这些值.名称和默认值是:

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


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


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):

有两个单独的文件大小限制.首先是struts.multipart.maxSize 来自 Struts 2default.properties 文件.出于安全原因,此设置存在禁止恶意用户上传超大文件到文件增加您的服务器磁盘空间.此设置默认为大约 2兆字节,应调整为最大文件大小(2 gigs max)您将需要该框架才能接收.如果您正在上传struts.multipart.maxSize 适用于一个表单上的多个文件合并的总数,而不是单个文件的大小.另一种设定,maximumSize,是一个拦截器设置,用于确保特定操作未收到太大的文件.注意以下示例中两个设置的位置:

<?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>


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


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.

这篇关于从 Struts 2 文件上传实用程序发布 ajaxForm 在 IE 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:41