限制Struts2文件上传最大大小而不上传整个文件

Ice*_*ras 4 java upload struts2 max

我试图在JSP/Struts2中实现文件上传,我注意到一个奇怪的行为.我在struts.xml中以这种方式声明了我的操作,将文件大小限制为1MB

<action name="massInsert" class="massInsertAction">
    <interceptor-ref name="fileUpload">
        <param name="allowedTypes">
             image/png,image/gif,image/jpeg
        </param>
        <param name="maximumSize">1000000</param>
    </interceptor-ref>
    <interceptor-ref name="defaultStack"/>

    <result name="success">/WEB-INF/jsp/massInsert/massInsert.jsp</result>
    <result name="validationError">/WEB-INF/jsp/massInsert/massInsert.jsp</result>
</action>
Run Code Online (Sandbox Code Playgroud)

它工作得很好,非图像文件和超过1MB的图像会引发错误.唯一的问题是,在删除之前,太大的文件无论如何都要完全上传到服务器临时文件夹中.

一旦达到限制,有没有办法停止上传?

编辑: Quaternion的解决方案有效,当请求超过最大集合时,下面的行会抛出错误,一切都会停止.没有文件写入磁盘

<constant name="struts.multipart.maxSize" value="1000000" />
Run Code Online (Sandbox Code Playgroud)

Qua*_*ion 5

有两个文件大小参数,一个与单个文件大小,另一个与最大多部分文件大小.这是因为你可以接收一个文件数组(只需将setter类型从File改为File [],这很简单),比如struts.multipart.maxSize设置为10MB并设置文件大小(maximumSize)到1 MB你应该能够收到10个1MB的文件.因此缓冲区应该允许增长到10 MB.

<?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>
Run Code Online (Sandbox Code Playgroud)

资料来源:https://cwiki.apache.org/confluence/display/WW/File+Upload#FileUpload-FileSizeLimits