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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4821334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:10:49  来源:igfitidea点击:

Limit Struts2 file upload max size without uploading the whole file

javauploadstruts2max

提问by IceGras

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

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

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.

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

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" />

回答by Quaternion

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.maxSizeis 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.

有两个文件大小参数,一个与单个文件大小有关,另一个与最大多部分文件大小有关。这是因为如果你愿意,你可以接收一个文件数组(只需将 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>

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

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