FileUploadInterceptor没有拦截大文件

时间:2013-09-02 12:05:45

标签: struts2

上传大文件后,我只在控制台收到警告信息,但没有阻止我,信息如下:

  

WARN FileUploadInterceptor:56 - 要上传的文件很大:   _7fa0eae6b3a9b769f938dd52c0fde541_imgThumb“170587_10150170888553504_2474786_o.jpg”   “upload__24f6477e_140de810828__8000_00000008.tmp”415411

这是我的Struts.xml设置:

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

<package name="Image" extends="json-default,struts-default,my-default">
    ...
    <action name="saveImage" class="example.Test" method="saveImg"> 
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <interceptor-ref name="exception"/>
        <interceptor-ref name="fileUpload">
            <param name="maximumSize">1</param>
            <param name="allowedTypes">image/tiff,image/jpeg</param>
        </interceptor-ref>
        <interceptor-ref name="params" />
        <interceptor-ref name="validation" />
        <interceptor-ref name="json">
            <param name="contentType">application/json</param>
        </interceptor-ref>
        <result name="success" type="json">
            <param name="root">save</param>
        </result>
        <result name="error" type="json">
            <param name="root">save</param>
        </result>
        <result name="invalid.token">/WEB-INF/jsp/invalidToken.jsp</result>
    </action>

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您要包括两次文件上传拦截器,因为它已经存在于默认堆栈中。

The Default Stack is defined as:

<!-- A complete stack with all the common interceptors in place.
         Generally, this stack should be the one you use, though it
         may do more than you need. Also, the ordering can be
         switched around (ex: if you wish to have your servlet-related
         objects applied before prepare() is called, you'd need to move
         servletConfig interceptor up.

         This stack also excludes from the normal validation and workflow
         the method names input, back, and cancel. These typically are
         associated with requests that should not be validated.
-->

<interceptor-stack name="defaultStack">
    <interceptor-ref name="exception"/>
    <interceptor-ref name="alias"/>
    <interceptor-ref name="servletConfig"/>
    <interceptor-ref name="i18n"/>
    <interceptor-ref name="prepare"/>
    <interceptor-ref name="chain"/>
    <interceptor-ref name="scopedModelDriven"/>
    <interceptor-ref name="modelDriven"/>
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="checkbox"/>
    <interceptor-ref name="multiselect"/>
    <interceptor-ref name="staticParams"/>
    <interceptor-ref name="actionMappingParams"/>
    <interceptor-ref name="params">
        <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
    </interceptor-ref>
    <interceptor-ref name="conversionError"/>
    <interceptor-ref name="validation">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="workflow">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="debugging"/>
</interceptor-stack>

那么你实际为行动定义的是

<action name="saveImage" class="example.Test" method="saveImg">     

    <!-- DEFAULT STACK IMPORT EXPLODED -->
    <interceptor-ref name="exception"/>
    <interceptor-ref name="alias"/>
    <interceptor-ref name="servletConfig"/>
    <interceptor-ref name="i18n"/>
    <interceptor-ref name="prepare"/>
    <interceptor-ref name="chain"/>
    <interceptor-ref name="scopedModelDriven"/>
    <interceptor-ref name="modelDriven"/>
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="checkbox"/>
    <interceptor-ref name="multiselect"/>
    <interceptor-ref name="staticParams"/>
    <interceptor-ref name="actionMappingParams"/>
    <interceptor-ref name="params">
        <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
    </interceptor-ref>
    <interceptor-ref name="conversionError"/>
    <interceptor-ref name="validation">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="workflow">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="debugging"/>

    <!-- YOUR ADDITIONAL SETTINGS -->
    <interceptor-ref name="exception"/>
    <interceptor-ref name="fileUpload">
        <param name="maximumSize">1</param>
        <param name="allowedTypes">image/tiff,image/jpeg</param>
    </interceptor-ref>
    <interceptor-ref name="params" />
    <interceptor-ref name="validation" />
    <interceptor-ref name="json">
        <param name="contentType">application/json</param>
    </interceptor-ref>
    <result name="success" type="json">
        <param name="root">save</param>
    </result>
    <result name="error" type="json">
        <param name="root">save</param>
    </result>
    <result name="invalid.token">/WEB-INF/jsp/invalidToken.jsp</result>
</action>

如您所见,许多拦截器被定义了两次。只需创建一个自定义拦截器堆栈,并在您需要的所有操作中使用它,或手动指定拦截器而不包括默认堆栈。

这样的东西
    <interceptor-ref name="exception"/>
    <interceptor-ref name="alias"/>
    <interceptor-ref name="servletConfig"/>
    <interceptor-ref name="i18n"/>
    <interceptor-ref name="prepare"/>
    <interceptor-ref name="chain"/>
    <interceptor-ref name="scopedModelDriven"/>
    <interceptor-ref name="modelDriven"/>
    <interceptor-ref name="fileUpload">
        <param name="maximumSize">1</param>
        <param name="allowedTypes">image/tiff,image/jpeg</param>
    </interceptor-ref>
    <interceptor-ref name="checkbox"/>
    <interceptor-ref name="multiselect"/>
    <interceptor-ref name="staticParams"/>
    <interceptor-ref name="actionMappingParams"/>
    <interceptor-ref name="params">
        <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
    </interceptor-ref>
    <interceptor-ref name="conversionError"/>
    <interceptor-ref name="validation">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="workflow">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="debugging"/>
    <interceptor-ref name="json">
        <param name="contentType">application/json</param>
    </interceptor-ref>