【问题标题】:How can I get file uploads with struts 2 working for large files?如何使用适用于大文件的 struts 2 上传文件?
【发布时间】:2012-09-23 09:41:47
【问题描述】:

我想使用struts2fileUpload 开发一个带有文件上传机制的网络应用程序,它的行为很奇怪,我不知道为什么。

我在struts.xml中配置了fileupload机制:

<package name="com.actions" namespace="/" extends="struts-default">
<action name="excelupload" class="com.actions.FileuploadAction">
    <interceptor-ref name="fileUpload">
            <param name="maximumSize">10000000</param>
    </interceptor-ref> 
        <result name="success">/fileupload.jsp</result>
        <result name="input">/fileupload.jsp</result>

    </action>
</package>

对应的JSP:

<s:form action="excelupload" method="post" enctype="multipart/form-data">
    <s:file name="excelfile" label="file" />
    <s:submit name="upload" value="upload" align="center" />
</s:form>

对应的Action:

public class FileuploadAction extends ActionSupport{
    File excelfile;
    public File getExcelfile() {
    return excelfile;
}

public void setExcelfile(File excelfile) {
    this.excelfile = excelfile;
}
    public String execute(){
        System.out.println(excelfile.getName());
        return SUCCESS;
    }
}

当我上传一个小文件时,一切正常。但是当我尝试上传大于 2 MB 的文件时,应用程序会抛出以下异常:

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

为了解决这个问题,我尝试添加struts-default.properties

struts.multipart.maxSize=20097152

到我的应用程序类路径的根目录。

修复了异常,但现在excelfile 始终为null,无论文件有多大。

有人知道我做错了什么吗?

编辑:对于部署,我使用 Tomcat 编辑:添加了缺少的 getter 和 setter 方法。

【问题讨论】:

  • 检查您正在使用的服务器的最大文件上传大小限制。
  • 我已经在 struts.xml 中使用 进行了尝试
  • 你的File excelFile有getter和setter吗?
  • 是的,由eclipse自动构建。
  • 您可能需要在操作中设置文件的 MIME 类型。浏览此链接,希望对您有所帮助。 struts.apache.org/2.2.3/docs/file-upload.html

标签: java tomcat file-upload struts2 gradle


【解决方案1】:

我可以通过删除来解决这个问题

<interceptor-ref name="fileUpload">
        <param name="maximumSize">10000000</param>
</interceptor-ref>

来自struts.xml

【讨论】:

    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 2012-06-22
    • 2014-10-17
    相关资源
    最近更新 更多