Struts2 文件上传大小限制

6 篇文章 0 订阅

struts2 上传框架有COS,pell,Common-FileUpload等几种,可以通过配置struts.properties中配置,如下:

struts.multipart.parser=cos

struts.multipart.parser=pell

struts.multipart.parser=jakarta //Struts 2 默认使用Jakarta的Common-FileUpload的文件上传解析器


在struts2 自带的文件default.properties中设置的(系统默认大小2M)
struts.multipart.maxSize=2097152
这个可以在struts.propertise 文件中修改 

Struts2 先通过Common-FileUpload文件上传解析器来对文件大小过滤,

Common-FileUpload包的FileUploadBase.class中parseRequest方法中进行了大小限制,其中代码如下:

        if (sizeMax >= 0 && requestSize > sizeMax) {
            throw new SizeLimitExceededException(
                "the request was rejected because its size (" + requestSize
                + ") exceeds the configured maximum (" + sizeMax + ")",
                requestSize, sizeMax);
        }

这里的sizeMax就是struts.multipart.maxSize的值。在上传文件之前系统会去比较文件的大小是否超过了该值,如果超过将抛
出上述异常,commons-fileupload组件是不支持国际化的,所以我们看到的异常都是默认的。


然后会进入struts2核心库里的org.apache.struts2.intercepto.FileUploadInterceptor中对大小进行拦截。这个大小值在struts.xml中配置,如下:

<interceptor-ref name="fileUpload">

<param name="maximumSize">409600</param> 单个上传文件最大不能超过400K
<param name="allowedTypes">...</param> mime类型,多个用逗号分开

</interceptor-ref>
** 加入默认的拦截器
<interceptor-ref name="defaultStack" />
FileUploadInterceptor.class中过滤代码如下:

    /**
     * Override for added functionality. Checks if the proposed file is acceptable based on contentType and size.
     *
     * @param file        - proposed upload file.
     * @param contentType - contentType of the file.
     * @param inputName   - inputName of the file.
     * @param validation  - Non-null ValidationAware if the action implements ValidationAware, allowing for better
     *                    logging.
     * @param locale
     * @return true if the proposed file is acceptable by contentType and size.
     */
    protected boolean acceptFile(File file, String contentType, String inputName, ValidationAware validation, Locale locale) {
        boolean fileIsAcceptable = false;

        // If it's null the upload failed
        if (file == null) {
            String errMsg = getTextMessage("struts.messages.error.uploading", new Object[]{inputName}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else if (maximumSize != null && maximumSize.longValue() < file.length()) {
            String errMsg = getTextMessage("struts.messages.error.file.too.large", new Object[]{inputName, file.getName(), "" + file.length()}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else if ((! allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {
            String errMsg = getTextMessage("struts.messages.error.content.type.not.allowed", new Object[]{inputName, file.getName(), contentType}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else {
            fileIsAcceptable = true;
        }

        return fileIsAcceptable;
    }
这里的maximumSize指的是Struts.xml中的maximumSize,如果没有配置,值为null。

fileUpload拦截器只是当文件上传到服务器上之后,才进行的文件类型和大小判断。 如果上传的文件大小刚好这struts.multipart.maxSize与maximumSize 之
间会抛出 key为struts.messages.error.file.too.large 对应的异常信息。

注意:maximumSize不能比struts.multipart.maxSize大,否则没有过滤效果。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值