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

发布于 2024-10-15 01:58:01 字数 933 浏览 2 评论 0原文

我正在尝试在 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>

效果很好,非图像文件和超过 1MB 的图像会引发错误。 唯一的问题是太大的文件在被删除之前已经完全上传到服务器临时文件夹中。

有没有办法在达到限制后立即停止上传?

编辑: 四元数的解决方案有效,当请求超过以下行的最大设置时,会抛出错误并且一切都会停止。没有文件写入磁盘

<constant name="struts.multipart.maxSize" value="1000000" />

I am trying to implement a file upload in JSP/Struts2, and I noted a strange behaviour.
I declared my action that way in struts.xml to limit file size to 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>

it works pretty well, non image files and images over 1MB throw an error.
The only issue is that the file that was too big got fully uploaded on the server temp folder anyway before being deleted.

Is there a way to stop the uploading as soon as the limit is hit ?

Edit:
Quaternion's solution works, when the request goes over the maximum set with the following line an error is thrown and everything stops. No file is written to disk

<constant name="struts.multipart.maxSize" value="1000000" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2024-10-22 01:58:01

有两个文件大小参数,一个与单个文件大小有关,另一个与最大多部分文件大小有关。这是因为您可以根据需要接收文件数组(只需将设置器类型从 File 更改为 File[],非常简单),假设 struts.multipart.maxSize 设置为 10MB 并且文件大小 (ma​​ximumSize) 设置为 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>

来源:https://cwiki.apache.org/confluence/显示/WW/File+Upload#FileUpload-FileSizeLimits

There are two file size parameters one has to do with individual files sizes the other with the the maximum multi part file size. This is in place because you can receive an array of files if you wish (just change the setters type from File to File[], so easy), say struts.multipart.maxSize is set to 10MB and file size (maximumSize) is set to 1 MB you should be able to receive 10 1MB files. So the buffer should be allowed to grow to 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>

Source: https://cwiki.apache.org/confluence/display/WW/File+Upload#FileUpload-FileSizeLimits

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文