struts2文件过滤拦截器fileUpload以及各种文件类型

分为两种方式吧,一种注解,一种配置struts.xml

基于方法

 @Action(interceptorRefs={@InterceptorRef("validation"), @InterceptorRef("defaultStack")}

基于类

  1. @InterceptorRefs({  
  2.     @InterceptorRef (  
  3.         value="fileUpload",  
  4.         params={"allowedTypes""application/msword"}  
  5.     ),  
  6.     @InterceptorRef (value="defaultStack")  
  7. })  

需要注意的是:必须加上defaultStack,而且defaultStack要放在最后

加上defaultStack的原因是 这个拦截器可以负责传参,参看 http://www.cnblogs.com/lanhj/p/3321890.html

defaultStack包含了fileUpload,但是defaultStack中的fileUpload是没有参数的,也就是支持任意类型的文件,

而xml的机制是:在重复出现同一个拦截器时,以第一个出现的为准(Annotation也会翻译成xml)

因此当defaultStack放在最前边时,默认的fileUpload会覆盖咱们的fileUpload

  1. <package name="default" extends="struts-default">  
  2.     <action name="upload2" class="edu.wuli.action.UploadAction">  
  3.         <param name="savePath">/homework</param>  
  4.         <result name="input">/WEB-INF/content/wrong.jsp</result>  
  5.         <result type="redirect">homework!query?article.id=${article.id}</result>  
  6.         <interceptor-ref name="fileUpload">    
  7.             <param name="allowedTypes">    
  8.                 application/msword   
  9.             </param>    
  10.             <param name="maxinumSize">3</param>    
  11.         </interceptor-ref>    
  12.         <interceptor-ref name="defaultStack"/>    
  13.     </action>  
  14. </package>  

需要注意的是action的name别符合零配置,否则不走这段配置,而是直接走Action

怎么输出fileUpload的错误信息?

需要使用i18n的资源文件,然后用<s:fielderror/>可以输出错误信息。

例:

在xml中加入:

  1. <constant name="struts.custom.i18n.resources" value="mess"/>  

然后在src目录下新建mess.properties,在里边加上:

  1. struts.messages.error.content.type.not.allowed=upload file contenttype is invalidate  
  2. struts.messages.error.file.too.large=upload file is too large  
  3. struts.messages.error.uploading=upload file uploading error  

三行分别代表:格式错误,文件容量过大, 上传错误

如果想输出中文,就需要转成Unicode码:

  1. struts.messages.error.content.type.not.allowed=\u6587\u4EF6\u7C7B\u578B\u9519\u8BEF  
  2. struts.messages.error.file.too.large=\u6587\u4EF6\u8FC7\u5927  
  3. struts.messages.error.uploading=\u4E0A\u4F20\u9519\u8BEF  

fileUpload文件容量设置(以字节为单位,以下限制1M)

  1. <param name="maximumSize">1048576</param>   

):

各种文件类型:

  1. '.a'      : 'application/octet-stream',      
  2.   
  3. '.ai'     : 'application/postscript',      
  4.   
  5. '.aif'    : 'audio/x-aiff',      
  6.   
  7. '.aifc'   : 'audio/x-aiff',      
  8.   
  9. '.aiff'   : 'audio/x-aiff',      
  10.   
  11. '.au'     : 'audio/basic',      
  12.   
  13. '.avi'    : 'video/x-msvideo',      
  14.   
  15. '.bat'    : 'text/plain',      
  16.   
  17. '.bcpio' : 'application/x-bcpio',      
  18.   
  19. '.bin'    : 'application/octet-stream',      
  20.   
  21. '.bmp'    : 'image/x-ms-bmp',      
  22.   
  23. '.c'      : 'text/plain',     
  24.   
  25. '.cdf'    : 'application/x-cdf',      
  26.   
  27. '.cdf'    : 'application/x-netcdf',      
  28.   
  29. '.cpio'   : 'application/x-cpio',      
  30.   
  31. '.csh'    : 'application/x-csh',      
  32.   
  33. '.css'    : 'text/css',      
  34.   
  35. '.dll'    : 'application/octet-stream',      
  36.   
  37. '.doc'    : 'application/msword',      
  38.   
  39. '.dot'    : 'application/msword',      
  40.   
  41. '.dvi'    : 'application/x-dvi',      
  42.   
  43. '.eml'    : 'message/rfc822',      
  44.   
  45. '.eps'    : 'application/postscript',      
  46.   
  47. '.etx'    : 'text/x-setext',      
  48.   
  49. '.exe'    : 'application/octet-stream',      
  50.   
  51. '.gif'    : 'image/gif',      
  52.   
  53. '.gtar'   : 'application/x-gtar',      
  54.   
  55. '.h'      : 'text/plain',      
  56.   
  57. '.hdf'    : 'application/x-hdf',      
  58.   
  59. '.htm'    : 'text/html',      
  60.   
  61. '.html'   : 'text/html',      
  62.   
  63. '.ief'    : 'image/ief',      
  64.   
  65. '.jpe'    : 'image/jpeg',      
  66.   
  67. '.jpeg'   : 'image/jpeg',      
  68.   
  69. '.jpg'    : 'image/jpeg',      
  70.   
  71. '.js'     : 'application/x-javascript',      
  72.   
  73. '.ksh'    : 'text/plain',      
  74.   
  75. '.latex' : 'application/x-latex',      
  76.   
  77. '.m1v'    : 'video/mpeg',      
  78.   
  79. '.man'    : 'application/x-troff-man',      
  80.   
  81. '.me'     : 'application/x-troff-me',      
  82.   
  83. '.mht'    : 'message/rfc822',      
  84.   
  85. '.mhtml' : 'message/rfc822',      
  86.   
  87. '.mif'    : 'application/x-mif',      
  88.   
  89. '.mov'    : 'video/quicktime',      
  90.   
  91. '.movie' : 'video/x-sgi-movie',      
  92.   
  93. '.mp2'    : 'audio/mpeg',      
  94.   
  95. '.mp3'    : 'audio/mpeg',      
  96.   
  97. '.mpa'    : 'video/mpeg',      
  98.   
  99. '.mpe'    : 'video/mpeg',      
  100.   
  101. '.mpeg'   : 'video/mpeg',      
  102.   
  103. '.mpg'    : 'video/mpeg',      
  104.   
  105. '.ms'     : 'application/x-troff-ms',      
  106.   
  107. '.nc'     : 'application/x-netcdf',      
  108.   
  109. '.nws'    : 'message/rfc822',      
  110.   
  111. '.o'      : 'application/octet-stream',      
  112.   
  113. '.obj'    : 'application/octet-stream',      
  114.   
  115. '.oda'    : 'application/oda',      
  116.   
  117. '.p12'    : 'application/x-pkcs12',      
  118.   
  119. '.p7c'    : 'application/pkcs7-mime',      
  120.   
  121. '.pbm'    : 'image/x-portable-bitmap',      
  122.   
  123. '.pdf'    : 'application/pdf',      
  124.   
  125. '.pfx'    : 'application/x-pkcs12',      
  126.   
  127. '.pgm'    : 'image/x-portable-graymap',      
  128.   
  129. '.pl'     : 'text/plain',      
  130.   
  131. '.png'    : 'image/png',      
  132.   
  133. '.pnm'    : 'image/x-portable-anymap',      
  134.   
  135. '.pot'    : 'application/vnd.ms-powerpoint',      
  136.   
  137. '.ppa'    : 'application/vnd.ms-powerpoint',      
  138.   
  139. '.ppm'    : 'image/x-portable-pixmap',      
  140.   
  141. '.pps'    : 'application/vnd.ms-powerpoint',      
  142.   
  143. '.ppt'    : 'application/vnd.ms-powerpoint',      
  144.   
  145. '.ps'     : 'application/postscript',      
  146.   
  147. '.pwz'    : 'application/vnd.ms-powerpoint',      
  148.   
  149. '.py'     : 'text/x-python',      
  150.   
  151. '.pyc'    : 'application/x-python-code',      
  152.   
  153. '.pyo'    : 'application/x-python-code',      
  154.   
  155. '.qt'     : 'video/quicktime',      
  156.   
  157. '.ra'     : 'audio/x-pn-realaudio',      
  158.   
  159. '.ram'    : 'application/x-pn-realaudio',      
  160.   
  161. '.ras'    : 'image/x-cmu-raster',      
  162.   
  163. '.rdf'    : 'application/xml',      
  164.   
  165. '.rgb'    : 'image/x-rgb',      
  166.   
  167. '.roff'   : 'application/x-troff',      
  168.   
  169. '.rtx'    : 'text/richtext',      
  170.   
  171. '.sgm'    : 'text/x-sgml',      
  172.   
  173. '.sgml'   : 'text/x-sgml',      
  174.   
  175. '.sh'     : 'application/x-sh',      
  176.   
  177. '.shar'   : 'application/x-shar',      
  178.   
  179. '.snd'    : 'audio/basic',      
  180.   
  181. '.so'     : 'application/octet-stream',      
  182.   
  183. '.src'    : 'application/x-wais-source',      
  184.   
  185. '.sv4cpio''application/x-sv4cpio',      
  186.   
  187. '.sv4crc' : 'application/x-sv4crc',      
  188.   
  189. '.swf'    : 'application/x-shockwave-flash',      
  190.   
  191. '.t'      : 'application/x-troff',      
  192.   
  193. '.tar'    : 'application/x-tar',      
  194.   
  195. '.tcl'    : 'application/x-tcl',      
  196.   
  197. '.tex'    : 'application/x-tex',      
  198.   
  199. '.texi'   : 'application/x-texinfo',      
  200.   
  201. '.texinfo''application/x-texinfo',      
  202.   
  203. '.tif'    : 'image/tiff',      
  204.   
  205. '.tiff'   : 'image/tiff',      
  206.   
  207. '.tr'     : 'application/x-troff',      
  208.   
  209. '.tsv'    : 'text/tab-separated-values',      
  210.   
  211. '.txt'    : 'text/plain',      
  212.   
  213. '.ustar' : 'application/x-ustar',      
  214.   
  215. '.vcf'    : 'text/x-vcard',      
  216.   
  217. '.wav'    : 'audio/x-wav',      
  218.   
  219. '.wiz'    : 'application/msword',      
  220.   
  221. '.wsdl'   : 'application/xml',      
  222.   
  223. '.xbm'    : 'image/x-xbitmap',      
  224.   
  225. '.xlb'    : 'application/vnd.ms-excel',      
  226.   
  227. '.xls'    : 'application/excel',      
  228.   
  229. '.xls'    : 'application/vnd.ms-excel',      
  230.   
  231. '.xml'    : 'text/xml',      
  232.   
  233. '.xpdl'   : 'application/xml',      
  234.   
  235. '.xpm'    : 'image/x-xpixmap',      
  236.   
  237. '.xsl'    : 'application/xml',      
  238.   
  239. '.xwd'    : 'image/x-xwindowdump',      
  240.   
  241. '.zip'    : 'application/zip',  
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值