本文某些内容复制自:http://zhidao.baidu.com/link?url=F0Z-FqbZ83BOj_xXp_B8rgJDzUoeVSWGgXwPNP5fEdLU1nvBK7yO4vnX_qfbJ-6HkDPKGtTh2MWt22-mgWTWXq

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

@InterceptorRefs({
@InterceptorRef (
value="fileUpload",
params={"allowedTypes", "application/msword"}
),
@InterceptorRef (value="defaultStack")
})

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

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

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

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

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

    <package name="default" extends="struts-default">
<action name="upload2" class="edu.wuli.action.UploadAction">
<param name="savePath">/homework</param>
<result name="input">/WEB-INF/content/wrong.jsp</result>
<result type="redirect">homework!query?article.id=${article.id}</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
application/msword
</param>
<param name="maxinumSize">3</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
</package>

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

怎么输出fileUpload的错误信息?

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

例:

在xml中加入:

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

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

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

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

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

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

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

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

):

各种文件类型:

'.a'      : 'application/octet-stream',    

'.ai'     : 'application/postscript',    

'.aif'    : 'audio/x-aiff',    

'.aifc'   : 'audio/x-aiff',    

'.aiff'   : 'audio/x-aiff',    

'.au'     : 'audio/basic',    

'.avi'    : 'video/x-msvideo',    

'.bat'    : 'text/plain',    

'.bcpio' : 'application/x-bcpio',    

'.bin'    : 'application/octet-stream',    

'.bmp'    : 'image/x-ms-bmp',    

'.c'      : 'text/plain',   

'.cdf'    : 'application/x-cdf',    

'.cdf'    : 'application/x-netcdf',    

'.cpio'   : 'application/x-cpio',    

'.csh'    : 'application/x-csh',    

'.css'    : 'text/css',    

'.dll'    : 'application/octet-stream',    

'.doc'    : 'application/msword',    

'.dot'    : 'application/msword',    

'.dvi'    : 'application/x-dvi',    

'.eml'    : 'message/rfc822',    

'.eps'    : 'application/postscript',    

'.etx'    : 'text/x-setext',    

'.exe'    : 'application/octet-stream',    

'.gif'    : 'image/gif',    

'.gtar'   : 'application/x-gtar',    

'.h'      : 'text/plain',    

'.hdf'    : 'application/x-hdf',    

'.htm'    : 'text/html',    

'.html'   : 'text/html',    

'.ief'    : 'image/ief',    

'.jpe'    : 'image/jpeg',    

'.jpeg'   : 'image/jpeg',    

'.jpg'    : 'image/jpeg',    

'.js'     : 'application/x-javascript',    

'.ksh'    : 'text/plain',    

'.latex' : 'application/x-latex',    

'.m1v'    : 'video/mpeg',    

'.man'    : 'application/x-troff-man',    

'.me'     : 'application/x-troff-me',    

'.mht'    : 'message/rfc822',    

'.mhtml' : 'message/rfc822',    

'.mif'    : 'application/x-mif',    

'.mov'    : 'video/quicktime',    

'.movie' : 'video/x-sgi-movie',    

'.mp2'    : 'audio/mpeg',    

'.mp3'    : 'audio/mpeg',    

'.mpa'    : 'video/mpeg',    

'.mpe'    : 'video/mpeg',    

'.mpeg'   : 'video/mpeg',    

'.mpg'    : 'video/mpeg',    

'.ms'     : 'application/x-troff-ms',    

'.nc'     : 'application/x-netcdf',    

'.nws'    : 'message/rfc822',    

'.o'      : 'application/octet-stream',    

'.obj'    : 'application/octet-stream',    

'.oda'    : 'application/oda',    

'.p12'    : 'application/x-pkcs12',    

'.p7c'    : 'application/pkcs7-mime',    

'.pbm'    : 'image/x-portable-bitmap',    

'.pdf'    : 'application/pdf',    

'.pfx'    : 'application/x-pkcs12',    

'.pgm'    : 'image/x-portable-graymap',    

'.pl'     : 'text/plain',    

'.png'    : 'image/png',    

'.pnm'    : 'image/x-portable-anymap',    

'.pot'    : 'application/vnd.ms-powerpoint',    

'.ppa'    : 'application/vnd.ms-powerpoint',    

'.ppm'    : 'image/x-portable-pixmap',    

'.pps'    : 'application/vnd.ms-powerpoint',    

'.ppt'    : 'application/vnd.ms-powerpoint',    

'.ps'     : 'application/postscript',    

'.pwz'    : 'application/vnd.ms-powerpoint',    

'.py'     : 'text/x-python',    

'.pyc'    : 'application/x-python-code',    

'.pyo'    : 'application/x-python-code',    

'.qt'     : 'video/quicktime',    

'.ra'     : 'audio/x-pn-realaudio',    

'.ram'    : 'application/x-pn-realaudio',    

'.ras'    : 'image/x-cmu-raster',    

'.rdf'    : 'application/xml',    

'.rgb'    : 'image/x-rgb',    

'.roff'   : 'application/x-troff',    

'.rtx'    : 'text/richtext',    

'.sgm'    : 'text/x-sgml',    

'.sgml'   : 'text/x-sgml',    

'.sh'     : 'application/x-sh',    

'.shar'   : 'application/x-shar',    

'.snd'    : 'audio/basic',    

'.so'     : 'application/octet-stream',    

'.src'    : 'application/x-wais-source',    

'.sv4cpio': 'application/x-sv4cpio',    

'.sv4crc' : 'application/x-sv4crc',    

'.swf'    : 'application/x-shockwave-flash',    

'.t'      : 'application/x-troff',    

'.tar'    : 'application/x-tar',    

'.tcl'    : 'application/x-tcl',    

'.tex'    : 'application/x-tex',    

'.texi'   : 'application/x-texinfo',    

'.texinfo': 'application/x-texinfo',    

'.tif'    : 'image/tiff',    

'.tiff'   : 'image/tiff',    

'.tr'     : 'application/x-troff',    

'.tsv'    : 'text/tab-separated-values',    

'.txt'    : 'text/plain',    

'.ustar' : 'application/x-ustar',    

'.vcf'    : 'text/x-vcard',    

'.wav'    : 'audio/x-wav',    

'.wiz'    : 'application/msword',    

'.wsdl'   : 'application/xml',    

'.xbm'    : 'image/x-xbitmap',    

'.xlb'    : 'application/vnd.ms-excel',    

'.xls'    : 'application/excel',    

'.xls'    : 'application/vnd.ms-excel',    

'.xml'    : 'text/xml',    

'.xpdl'   : 'application/xml',    

'.xpm'    : 'image/x-xpixmap',    

'.xsl'    : 'application/xml',    

'.xwd'    : 'image/x-xwindowdump',    

'.zip'    : 'application/zip',

struts2文件过滤拦截器fileUpload以及各种文件类型的更多相关文章

  1. struts2中的方法过滤拦截器

    方法过滤拦截器是只过滤指定的方法,如果使用针对action 的普通的过滤器则会过滤该action内部 所有方法.如果在一个action中同时有多个作为业务逻辑控制的方法存在 的话则会过滤所有的业务逻辑 ...

  2. Struts2(十四)拦截器实现权限管理

    一.认识拦截器 拦截器也是一个类 拦截器可以在Action被调用之前和之后执行代码 框架很多核心功能是拦截器实现的 拦截器的特点: 拦截器自由组合,增强了灵活性.扩展性.有利于系统解耦 拦截器可以拦截 ...

  3. 十五、struts2中的拦截器(框架功能核心)

    十五.struts2中的拦截器(框架功能核心) 1.过滤器VS拦截器 功能是一回事. 过滤器是Servlet规范中的技术,可以对请求和响应进行过滤. 拦截器是Struts2框架中的技术,实现AOP(面 ...

  4. Struts2笔记_拦截器

    A.拦截器是什么 --- Interceptor:拦截器,起到拦截Action的作用. ---Filter:过滤器,过滤从客户端向服务器发送的请求. ---Interceptor:拦截器,拦截是客户端 ...

  5. java struts2入门学习---拦截器学习

    一.拦截器,拦截器栈 1.拦截器的作用 拦截器本质上和servlet的过滤器是一样的.在struts2中,拦截器能够对Action前后进行拦截,拦截器是一个可插拨的,你可以选择使用拦截器,也可以卸载拦 ...

  6. Struts2学习之拦截器

    © 版权声明:本文为博主原创文章,转载请注明出处 拦截器: - Struts2大多数核心功能都是通过拦截器实现的,每个拦截器完成某项功能 - 拦截器方法在Action执行之前或之后执行 工作原理: - ...

  7. 5.Struts2中的拦截器

    拦截器是Struts2中的核心,其自带很多很多的拦截器,这里主要介绍一下自定义拦截器,恩多一半情况下呢?我们不需要使用到自定义的拦截器,Struts2本身已经提 供了很多的拦截器供我们使用,对于自定义 ...

  8. Struts2之类范围拦截器和方法拦截器

    1.Struts2拦截器的体系结构 Struts2拦截器最大的特点是其透明性,即用户感觉不到它的存在,但我们在使用Struts2框架时,拦截器时时刻刻都在帮助我们处理很多事情. 包括: 文件上传 表单 ...

  9. struts2基础——自定义拦截器

    一.自定义拦截器 默认的拦截器能实现的功能是有限的,Struts2 支持自定义拦截器. 二.拦截器类 1.实现 Interceptor 接口 2.继承 AbstractInterceptor 抽象类, ...

随机推荐

  1. SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群

    session集群的解决方案: 1.扩展指定server 利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/ ...

  2. 《Unix环境高级编程》读书笔记 第7章-进程环境

    1. main函数 int main( int argc, char *argv[] ); argc是命令行参数的数目,包括程序名在内 argv是指向参数的各个指针所构成的数组,即指针数组 当内核执行 ...

  3. 常用类Math,StringBuffer,包装类,Date

    ----------- StringBuffer --------------- StringBuffer是字符串缓冲区. 是一个容器. 特点: 1,长度是可变化的. 2,可以直接操作多个数据类型. ...

  4. [agc011e]increasing numbers

    题意: 如果一个十进制非负整数的所有数位从高位到低位是不减的,我们称它为“上升数”,例如1558,11,3,0都是上升数,而10,20170312则不是: 给定整数N,求最小的k使得N能被表示为k个上 ...

  5. 支持JSONP跨域的对象

    支持JSONP跨域的对象 1:img 2:iframe 3:link 4:script 为什么,JSONP 最终选择是 script 实现呢?度娘来也! 平常我们进行JSONP请求数据,因为 json ...

  6. Oracle with as 嵌套

    oracle  with as可以理解为临时视图,可以极大的简化sql语句,并且支持嵌套使用. With c3 As(Select * From v_tr_daily Where p_date=to_ ...

  7. Java基础学习总结(3)——抽象类

    一.抽象类介绍 下面通过一下的小程序深入理解抽象类 因此在类Animal里面只需要定义这个enjoy()方法就可以了,使用abstract关键字把enjoy()方法定义成一个抽象方法,定义如下:pub ...

  8. iOS 时间类经常用法

    //当前日前日期 NSDate *today = [NSDate date]; //时区 NSTimeZone *zone = [NSTimeZone systemTimeZone]; //设置间隔 ...

  9. UITextField限制输入长度

    首先,汉字的输入时的联想词在输入到TextFiled时,并不会走 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersIn ...

  10. [JSOI2008] [BZOJ1567] Blue Mary的战役地图 解题报告 (hash)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1567 Description Blue Mary最近迷上了玩Starcraft(星际争霸 ...