struts2文件上传中,如何限制上传的文件类型
生活随笔
收集整理的這篇文章主要介紹了
struts2文件上传中,如何限制上传的文件类型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
來源:http://www.blogjava.net/landor2004/archive/2009/06/11/281416.html
這個在struts2的doc中已經有所說明,但是說得并不詳細,而且他給的例子是有錯誤的,下面我將列出文件上傳并限制類型的具體步驟
struts2版本是2.1.6
struts2是根據contentType來限制的,并不是文件的擴展名
比如我想僅上傳image/png,image/gif,image/jpeg這三種文件類型
第一種方法是通過javascript校驗來限制,這個比較簡單,獲取input的value然后截取擴展名進行判斷即可
第二種是根據struts2自帶的fileupload攔截器中提供的allowedTypes來進行限制,步驟如下:
1 配置fileupload攔截器
struts2的defaultStack中已經含有fileupload攔截器,如果想加入allowedTypes參數,需要從新寫一個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="debugging"/>
????????????????<interceptor-ref?name="profiling"/>
????????????????<interceptor-ref?name="scopedModelDriven"/>
????????????????<interceptor-ref?name="modelDriven"/>
????????????????<interceptor-ref?name="fileUpload">
??????????????????<param?name="allowedTypes">
?????????????????????image/png,image/gif,image/jpeg
??????????????????</param>
????????????????</interceptor-ref>
????????????????<interceptor-ref?name="checkbox"/>
????????????????<interceptor-ref?name="staticParams"/>
????????????????<interceptor-ref?name="actionMappingParams"/>
????????????????<interceptor-ref?name="params">
??????????????????<param?name="excludeParams">dojo/..*,^struts/..*</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-stack>
????????</interceptors>
????????<default-interceptor-ref?name="myDefaultStack"></default-interceptor-ref> 僅修改代碼中的
<interceptor-ref?name="fileUpload">
??????????????????<param?name="allowedTypes">
?????????????????????image/png,image/gif,image/jpeg
??????????????????</param>
????????????????</interceptor-ref>
上面配置的是上傳文件類型的限制,其實共有兩個參數
maximumSize?(可選)?-這個攔截器允許的上傳到action中的文件最大長度(以byte為單位).?注意這個參數和在webwork.properties中定義的屬性沒有關系,默認2MB
allowedTypes?(可選)?-以逗號分割的contentType類型列表(例如text/html),這些列表是這個攔截器允許的可以傳到action中的contentType.如果沒有指定就是允許任何上傳類型.
2 jsp頁面定義如下( testFileUpload.jsp)
<s:form?action="testFileUpload"method="post"enctype="multipart/form-data">
????????<s:file?name="file"theme="simple"/>
????????<s:fielderror?name="file"></s:fielderror>
????????<s:submit/>
????</s:form> 3 后臺的action聲明如下(我用的是struts2的注解進行action配置)
publicclassTestFileUploadAction?extendsActionSupport{
????privateFile?file;
????privateString?fileContentType;
????privateString?fileFileName;
????@Action(
????????????value?="testFileUpload",?results?={
????????????????@Result(name?="input",?location?="/testFileUpload.jsp"),
????????????????@Result(name?="success",?location?="/testFileUploadSuccess.jsp")
????????????}
????)
????publicString?execute()?{
????????returnSUCCESS;
????}
get/set......
} 注意:如果jsp中file的name="xxx",那么后臺action中的屬性要做相應更改為
privateFile?xxx ;
???? privateString?xxx ContentType;
???? privateString?xxx FileName;
同時注意大小寫一定要一致
4 定義錯誤文件類型的消息提示,這個需要用到struts2的資源文件,在struts.properties文件中加入
struts.custom.i18n.resources=globalMessages globalMessages對應著資源文件名
5 在源文件夾下定義資源文件 globalMessages.properties,并在里面加入如下信息:
struts.messages.error.content.type.not.allowed=upload?file?contenttype?is?invalidate
這里稍作說明(拷貝一下struts2的幫助):
如果你的action實現了ValidationAware接口(如果action繼承了ActionSupport,那么就相當于實現了ValidationAware),這個攔截器就可以添加幾種字段錯誤.這些錯誤信息是基于存儲在struts-messages.properties文件中的一些i18n值,這個文件是所有i18n請求的默認文件.你可以在自己消息文件的復寫以下key的消息文 字
struts.messages.error.uploading?-文件不能上傳的通用錯誤信息
struts.messages.error.file.too.large?-上傳文件長度過大的錯誤信息
struts.messages.error.content.type.not.allowed?-當上傳文件不符合指定的contentType
以上配置完畢后,測試一下,對于非法的contentType,例如xxx.log這個文件的的contentType是pplication/octet-stream?
會給出提示: upload?file?contenttype?is?invalidate
總結
以上是生活随笔為你收集整理的struts2文件上传中,如何限制上传的文件类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css如何给文字加边框
- 下一篇: jquery.MultiFile 实现自