限制Struts2文件上传最大大小而不上传整个文件 [英] Limit Struts2 file upload max size without uploading the whole file

查看:106
本文介绍了限制Struts2文件上传最大大小而不上传整个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JSP / Struts2中实现文件上传,我注意到一个奇怪的行为。
我在struts.xml中声明我的行为,将文件大小限制为1MB

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>

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

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 ?

编辑:
当请求超过最大设置时,Quaternion的解决方案有效以下行引发错误,一切都停止。没有文件写入磁盘

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


推荐答案

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

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>

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

这篇关于限制Struts2文件上传最大大小而不上传整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆