文件上传拦截器未截获大文件

  • 本文关键字:文件 struts2
  • 更新时间 :
  • 英文 :


上传一个大文件后,我只在控制台中收到警告消息,但它并没有阻止我,消息如下:

WARN FileUploadInterceptor:56-要上传的文件太大:_7fa0eae6b3a9b769f938dd52c0de541_imgThumb"170587_10150170888553504_2474786_o.jpg"上传__24f6477e_140de810828__8000_0000000 8.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>

任何帮助都将不胜感激!

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

默认堆栈定义为:

<!-- 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>

最新更新