天天看點

Struts2上傳檔案的最大Size的設定

struts2中檔案上傳的二個限制,一個是struts.multipart.maxsize,如果不設定,struts2 的核心包下的default.properties檔案裡有預設的大小設定struts.multipart.maxsize=2097152,即2m. 這是struts2檔案上傳的第一道關。

第二道關是inteceptor中的maximumsize. 當真實的檔案大小能通過第一道關時.針對不同的action中配置的inteceptor,maximumsize才能發揮相應的攔截作用.

比如struts.multipart.maxsize=50m

actiona中inteceptora的maximumsize=30m

actionb中inteceptorb的maximumsize=10m

struts.multipart.maxsize=50m對于inteceptora,b都會起到第一關的作用.

而inteceptora和inteceptorb可以在通過第一關之後,根據自己的業務定制各自針對攔截器起作用的maximumsize

如果真實的檔案>50m. 抛出會抛出the request was rejected because its size (xxxx) exceeds the configured maximum (xxxx)異常,他是不能被國際化的,因為這個資訊是commons-fileupload元件抛出的,是不支援國際化這資訊。

源碼可以看struts2.2 org.apache.commons.fileupload.fileuploadbase.java

如果inteceptora上傳的是40m的真實檔案

那麼此時攔截器inteceptora會通路國際化資訊:struts.messages.error.file.too.larges對應的值.當且僅當上傳檔案<=30m的時候,inteceptora才會成功上傳。

下面是解決struts.multipart.maxsize提示資訊不友好的問題.

當超過50m時.commons-fileupload抛出運作時異常

struts2會把這個異常看到是action級别的異常.是以會将異常資訊

the request was rejected because its size (xxxx) exceeds the configured maximum (xxxx)寫到actionerror裡面。

我們需要做的就是在action裡覆寫addactionerror方法。

@override

public void addactionerror(string anerrormessage)

{

     //改從國際化裡取值

    if (anerrormessage.startswith("the request was rejected because its size"))

    {

        super.addactionerror(gettext("struts.multipart.maxsize.limit"));

    }

   else

   {

        super.addactionerror(anerrormessage);

   }

}

相應的配置檔案

struts.multipart.maxsize.limit=系統上傳的檔案最大為50m

struts.messages.error.file.too.larges=新廣告批量上傳的檔案最大為5m

struts.messages.error.content.type.not.allowed=上傳的檔案格式目前僅支援xls格式

struts.messages.error.uploading=上傳檔案失敗

struts.messages.invalid.token=您已經送出了表單,請不要重複送出。

fileupload.filenums.exceed=已經有超過5個檔案在運作,請稍候再試

filedownload.rows.exceed=由于您選擇的廣告組内廣告數量太多,請分組下載下傳

accountnotexist=客戶不存在

invalidtask=無效的任務

注意,由于inteceptor中途傳回,原來頁面上輸入的其他文本内容也都不見了,也就是說params注入失敗。

因為這個異常是在檔案上傳之前捕獲的,檔案未上傳,同時params也為注入,是以這時最好重定向到一個jsp檔案,提示上傳失敗,然後重寫填寫相應資訊。

解決辦法:最好跳到一個專門顯示錯誤的頁.而不要傳回操作頁。

注意,攔截器所謂的同名配置覆寫,是重複執行的,比如defaultstack中是包含fileupload,token的. 如果将<interceptor-ref name="defaultstack" />放到顯示定義的攔截器之後,會覆寫顯示定義的攔截器.。

<action name="batchmiadoperation!*" method="{1}"class="com.*****.***.action.multiidea.batchad.batchmiadoperationaction">

    <interceptor-ref name="defaultstack" />

    <interceptor-ref name="fileupload">

        <param name="maximumsize">5242880</param>

        <!--

        <param name="allowedtypes">

        application/vnd.ms-excel

        </param>

         -->

    </interceptor-ref>

    <interceptor-ref name="token">

        <param name="excludemethods">

           init,search,updatebatchcpcmatch,batchexportmiad,downloadwhenerror

    <result name="input">

         /web-inf/jsp/multiidea/batchad/batchmiad.jsp

    </result>

    <result name="success">

         /web-inf/jsp/multiidea/batchad/batchmiad.jsp

    <result name="invalid.token">

       /web-inf/jsp/multiidea/batchad/batchmiad.jsp

</action>

struts.xml相關配置如下:

<constant name="struts.multipart.maxsize" value="9000000"/>

<action name="fileupload" class="cn.timefly.strutstest.fileuploadaction">

    <result name="success">/fileuploadresult.jsp</result>

    <result name="input">/fileupload.jsp</result>

    <interceptor-ref name="fileupload">

      <param name="maximumsize">500000</param> 

      <param name="allowedtypes">application/vnd.ms-powerpoint</param>     

    <interceptor-ref name="defaultstack" /> 

<action name="fileupload" class="cn.timefly.strutstest.fileuploadaction2">

      <param name="maximumsize">300000</param> 

原帖位址:http://www.cnblogs.com/highriver/archive/2011/06/01/2065557.html