`

struts2文件上传如何限制上传文件类型(类型列表)

阅读更多

StarStreamstruts2文件上传如何限制上传文件类型(类型列表)

这个在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-stack name="myDefaultStack">

                <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配置)

public class TestFileUploadAction extends ActionSupport{

    private File file;

    private String fileContentType;

    private String fileFileName;

    @Action(

            value = "testFileUpload", results = {

                @Result(name = "input", location = "/testFileUpload.jsp"),

                @Result(name = "success", location = "/testFileUploadSuccess.jsp")

            }

    )

    public String execute() {

        return SUCCESS;

    }

    get/set......

}

注意:如果jsp中file的name="xxx",那么后台action中的属性要做相应更改为

    private File xxx;

    private String xxxContentType;

    private String xxxFileName;

同时注意大小写一定要一致

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

 

'.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',

 

分享到:
评论
1 楼 gpo 2011-11-28  
         很好 

相关推荐

Global site tag (gtag.js) - Google Analytics