struts2allowed文件后缀名|struts2文件上传中如何限制上传的文件类型

A. struts2上传文件的类型去哪里找

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中定义的属性没有关系,默认2MBallowedTypes (可选) – 以逗号分割的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=globalMessagesglobalMessages对应着资源文件名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-xwindowmp', '.zip' : 'application/zip',

B. Struts2的struts.properties文件在哪儿啊

struts.properties 是可以不要的!!!因为 struts.xml文件中 有 <constant> 这个节点, 你可以把你想写在struts.properties的自定义配置写在 struts.xml文件当中.例如,你想 开启 struts2 的开发模式可以直接在 struts.xml文件当中写上.<constant name="struts.devMode" value="true“>不用再去 去创建 一个 struts.properties,并写上 struts.devMode = true所有的配置项目,其实 你可以去看看 struts2 默认的配置文件 default.properties.位置在 struts2-core-2.x.x.jar 的 org.apache.struts2 包 下面的当然,你可能又会问了,如果同时都在两个文件配置了,一个相同的项目,哪个是有效的呢他其实是有一个加载顺序的: 先加载 struts.xml,再加载 struts.properties也就是说 struts.properties 是可以覆盖 struts.xml里面的 配置的,具体 是否 要 struts.properties ,还是得根据 具体情况,具体分析吧,不过 我基本上不用,免得 出现 配置混乱的情况.呵呵 PS: struts.xml 不能写成Struts.xml 哟 ,注意首字母 s不能写成大写了.

C. Struts2文件下载 火狐和谷歌浏览器 文件名后缀多出个 _(下划线),IE正常

您好!很高兴为您答疑!火狐下您可以使用Firebug检查下版页面代码,确认下Struts.xml 文件 &{fileName}名称及书写格式,看权下有没有错误。您可以在火狐社区了解更多内容。希望我的回答对您有所帮助,如有疑问,欢迎继续在本平台咨询。

D. struts2文件上传中,如何限制上传的文件类型

只需要在struts配置文件中配置就OK了案例如下:<package name="upload" extends="struts-default" namespace="/upload"><!– 配置 –><action name="upload" class="www.ijava.com.UploadAction" > <param name="savePath">e:/images/</param><!–往fileuploadInterceptor 注入 –><interceptor-ref name="defaultStack"><!– 改变当前文件运行上传的类型 –><param name="fileUpload.allowedTypes">image/jpeg,image/jpg</param><!– 允许的文件后缀 –><param name="fileUpload.allowedExtensions">jpg,jpeg,gif</param></interceptor-ref><result>/index.jsp</result></action>

E. 用Struts2做文件上传时,前台js怎样获得文件对象,文件名,文件内容类型(不用Bean的get,set方法)。

出于对用户的安全性考虑,js是无法访问上传文件的完整路径,也就是无法获得file对象,也不能获得文件内容类型fileContentType,但是可以获得文件名(传值或者getElementById,然后对该值进行切割得到文件名和后缀名),故可以获取后缀名,但与文件内容类型fileContentType可能不一致,所以要进行文件的类型,大小等判断,还需要传到后台进行。不过要是图片的话,js是有一个比较bt的做法可以获得大小(类型暂且用后缀名来进行判断,前面也说了获取文件名的方法)的,根据路径new一个Image对象,如var image = new Image(); image.src = url;(url就是文件上传的路径),image.fileSize就是图片的大小

F. 为什么Struts2上传文件之后扩展名变成了tmp,如何取得文件正确的扩展名

在action中获取到上传文件名利用上传文件名来存储就不会出现tmp结尾文件了


赞 (0)