struts2 文件上傳內容_文件大小和類型的攔截

 
設置Struts2文件上傳的類型和大小:

在struts.xml中的Action中配置如下:

<!-- 國際化文件 -->
<constant name="struts.custom.i18n.resources" value="message"></constant>

<action name="upload" class="com.mengya.action.UploadAction">
<result name="success">/result.jsp</result>
<!-- 若上傳的文件不符合要求則返回input -->
<result name="input">/index.jsp</result>

<!-- 限制上傳的文件的大小,在struts-default.xml中我們可能找到一個fileUpload攔截器 -->
<interceptor-ref name="fileUpload">
<!-- 單位爲字節,這裏爲400k 這裏的大小是指每個文件上傳的大小,若多個文件上傳指每一個文件的大小 -->
<param name="maximumSize">409600</param>
<!-- 指定文件上傳的類型,在X:\Program Files\Apache Software Foundation\Tomcat 6.0\conf\web.xml中Tomcat提定所有文件類型 -->
<!-- word類型 -->
<param name="allowedTypes">
application/msword
</param>
</interceptor-ref>
<!-- 加上默認攔截器 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

在message.properties文件中加上客戶化的文件上傳提示
#文件上傳類型不允許
struts.messages.error.content.type.not.allowed = \u4e0a\u4f20\u6587\u4ef6\u7c7b\u578b\u4e0d\u6b63\u786e\uff0c\u8bf7\u91cd\u8bd5\uff01
#文件上傳的大小不允許
struts.messages.error.file.too.large = \u4e0a\u4f20\u6587\u4ef6\u592a\u5927\uff01
#這些信息可以在struts2-core-2.1.6.jar下面的struts-message.properties文件裏找到
 
來自於:http://www.blogjava.net/winderain/archive/2011/01/04/310971.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章