Struts2中上传文件配置信息

来源:互联网 发布:数据策划师 编辑:程序博客网 时间:2024/05/16 05:21
在struts.xml中配置,使用的是拦截器fileUploadStack 
具体的配置如下: 
Xml代码  收藏代码
  1. <constant name="struts.multipart.maxSize" value="1024000"></constant>  
  2.       
  3.         <action name="demo" class="action.file.FileUploadAction">  
  4.             <interceptor-ref name="fileUploadStack">  
  5.                 <param name="maximumSize">1024000</param>  
  6.                 <param name="fileUpload.allowedTypes">  
  7.                     image/gif,image/png,image/bmp,image/pjpeg,image/jpg,image/jpeg  
  8.                 </param>  
  9.             </interceptor-ref>  
  10.             <result name="success" type="httpheader">  
  11.                 <param name="status">200</param>  
  12.             </result>  
  13.         </action>  

补充:只需在<action></action>中加入拦截器引用fileUpload或者fileUploadStack 
Xml代码  收藏代码
  1. <interceptor-ref name="fileUpload">  
  2.               <!-- 配置允许上传的文件类型,多个用","分隔 -->  
  3.               <param name="allowedTypes">  
  4.           image/bmp,image/png,image/gif,image/jpeg,image/jpg   
  5. ,image/x-png, image/pjpeg<o:p></o:p>  
  6.   
  7.               </param>  
  8.               <!-- 配置允许上传的文件大小,单位字节 -->  
  9.               <param name="maximumSize">102400</param>  
  10.            </interceptor-ref>  

struts2 上传文件时,如果限制了上传的文件类型,当用户上传被限制的类型时,struts默认显示的错误如下:
Content-Type not allowed: file "icudt42.dll" "upload_47e43c5c_1266edfc786__8000_00000002.tmp" application/x-msdownload
但这种显示不是很友好,要自定义该错误,需要做的是:
1 在struts.xml中添加<constant name="struts.custom.i18n.resources" value="struts" />
(不知道为什么)
2 在src下新建struts.properties写上
struts.messages.error.content.type.not.allowed=文件类型不正确
struts.messages.error.file.too.large=上传的文件太大
struts.messages.error.uploading=上传出错
注释:在properties文件中中文是乱码的,要用native2ascii转一下