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

來源:互聯網
上載者:User

   

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
        </param>
    </interceptor-ref>
    <result name="input">
         /WEB-INF/jsp/multiidea/batchad/BatchMIAD.jsp
    </result>
    <result name="success">
         /WEB-INF/jsp/multiidea/batchad/BatchMIAD.jsp
    </result>
    <result name="invalid.token">
       /WEB-INF/jsp/multiidea/batchad/BatchMIAD.jsp
    </result>
</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>
    <interceptor-ref name="defaultStack" /> 
</action>

 

<action name="FileUpload" class="cn.timefly.strutsTest.FileUploadAction2">
    <result name="success">/FileUploadResult.jsp</result>
    <result name="input">/FileUpload.jsp</result>
    <interceptor-ref name="fileUpload">
      <param name="maximumSize">300000</param> 
      <param name="allowedTypes">application/vnd.ms-powerpoint</param>     
    </interceptor-ref>
    <interceptor-ref name="defaultStack" /> 
</action>

 

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

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.