我试图在 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" />
最佳答案
有两个文件大小参数,一个与单个文件大小有关,另一个与最大多部分文件大小有关。这是因为您可以根据需要接收文件数组(只需将 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>
来源:https://cwiki.apache.org/confluence/display/WW/File+Upload#FileUpload-FileSizeLimits
关于java - 限制Struts2文件上传最大大小而不上传整个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4821334/