本文某些内容复制自: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学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理

    在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...

  2. 洛谷2114 bzoj3668[NOI2014]起床困难综合症

    题目描述 21世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm一直坚持与起床困难综合症作斗争.通过研究相关文献,他找到了该病的发病原因 ...

  3. root of factory hierarchy

    项目编译错误! project---->clean

  4. python基础6(函数 Ⅰ)

    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段 定义 def function_name(args...): function_body #例子 def print_somethin ...

  5. Maven copy方式列举

    maven copy有很多种方法: 1.maven-antrun-plugin (使用ant复制) <build> <finalName>flex</finalName& ...

  6. 【hdu 1890】Robotic Sort

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给你n个数字; i从1到n; 每次让你把第i小的数和数组的第i个元素之间这段区间内 ...

  7. 4.有关日期格式属性改动常识,v$nls_parameters,between and,查询指定部门的员工信息,in和null,like模糊查询,order by后面能够跟:列名、表达式、别名、序号

     1 有关日期格式属性改动常识 NLS_DATE_FORMAT           DD-MON-RR select sysdate from dual; NLS_CURRENCY         ...

  8. Android里使用正則表達式

    在Android里怎样使用正則表達式: 以验证username为例.username一般字母开头,同意字母数字下划线.5-16个字节: String regEx = "^[a-zA-Z][a ...

  9. mysql-面试题目1

    一.数据库的ACID 原子性(Atomicity):保证事务中的所有操作全部执行或全部不执行. 一致性(Consistency):保证数据库始终保持数据的一致性——事务操作之前和之后都是一致的. 隔离 ...

  10. programming-challenges Shoemaker&#39;s Problem (110405) 题解

    Greedy. 证明: Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., ...