读书人

Struts2文件下传类型校验有关问题(Val

发布时间: 2012-10-26 10:30:59 作者: rapoo

Struts2文件上传类型校验问题(Validate)

问题:

????? 最近在做Struts2上传图片时,遇到文件类型校验问题。系统中只允许上传指定格式的问题,如:jpg,jpeg,png等,我按照以往的经验对Struts2的action配制文件进行了配制,在功能完成后对后其进行测试,在FF下工作正常,但在IE下却报找不到action中的validate(验证)方法。。

?

解决:

???????? 其实并不是找不到验证方法,问题是IE在图片上传过程中,把jpeg格式的文件翻译成了pjpeg,而我在struts的action配制文件中只配置了image/jpeg,并没有配置image/pjpeg,所以通不过struts2的默认validate(验证),所以就会报错。

?

详细信息:

在Struts2的Action配置文件中,加了Struts2默认fileUploadStack栏截器,并加了类型拦截参数,如下:

?

<action name="upLoadPhoto" method="upLoadPhoto">    <interceptor-ref name="fileUpload">    <param name ="allowedTypes">          image/png,image/gif,image/jpeg    </param >    <param name="maximumSize">            2097152    </param>      </interceptor-ref>    <interceptor-ref name ="fileUploadStack"/>    <interceptor-ref name = "defaultStack"/>    <result name="success" type="redirect">updatePhoto</result>    <result name="error" type="redirect">/user/toLogin.action</result></action>

?

以上配置在FF下没有任何问题,符合类型的图片都可能正确上传,但是在IE下会他如下错误:

2011-01-11 11:10:02 DEBUG -falling back to default type converter         [com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter@17d93a9]2011-01-11 11:10:02 DEBUG -Validating /albums/upLoadPhoto with         method upLoadPhoto.2011-01-11 11:10:02 DEBUG -Invoking validate() on action com.pyj.        web.ui.albums.PicAction@91e0a82011-01-11 11:10:02 DEBUG -cannot find method [validateUpLoadPhoto]         in action [com.pyj.web.ui.albums.PicAction@91e0a8]2011-01-11 11:10:02 DEBUG -cannot find method [validateDoUpLoadPhoto]         in action [com.pyj.web.ui.albums.PicAction@91e0a8]2011-01-11 11:10:02 DEBUG -Errors on action com.pyj.web.ui.albums.PicAction@91e0a8,         returning result name 'input'2011-01-11 11:10:02 WARN  -Could not find action or result        No result defined for action com.pyj.web.ui.albums.PicAction and result input

?

?从报错信息看是因为我在Action中没有配置validateUpLoadPhoto方法,没有通过Struts2的验证,所以Struts2自动返回input结果,但当我在Action中添加了validateUpLoadPhoto,方法体如下:

?

public void validateUpLoadPhoto(){     //空方法}

??

错误依然存在,接着我又在validateUpLoadPhoto方法中打出Struts2的所有错误信息:

?

public void validateUpLoadPhoto(){    for (String one : getActionErrors()) {        System.out.println(one);    }    for (String one : getActionMessages()) {        System.out.println(one);    }    Map<String, List<String>>  fieldErrors = this.getFieldErrors();        for (Entry<String, List<String>> one : fieldErrors.entrySet()) {            System.out.println(one.getKey()+":"+one.getValue());    }}

?

actionerror,actionmessage都为空,而fielderror却有错误信息,

?

phf:[Content-Type not allowed: phf "5.jpg" "upload__538d61dd_12d732b033c__8000_00000002.tmp" image/pjpeg]

?

可以看到,IE把jpeg类型的文件翻译成了pjpeg,到此问题基本已经找出,现在修改Action配制文件即可,如下:

?

<action name="upLoadPhoto" method="upLoadPhoto">    <interceptor-ref name="fileUpload">    <param name ="allowedTypes">          image/png,image/gif,image/jpeg,image/pjpeg    </param >    <param name="maximumSize">            2097152    </param>      </interceptor-ref>    <interceptor-ref name ="fileUploadStack"/>    <interceptor-ref name = "defaultStack"/>    <result name="success" type="redirect">updatePhoto</result>    <result name="error" type="redirect">/user/toLogin.action</result></action>

?到此问题解决,并不是没有validate()方法的原因,而是IE在文件上传时,翻译图片格式的问题。validateUpLoadPhoto方法默认可以不写,struts2的默认validate可以对图片进行验证。

?

附IE下文件上传,图片类型转换规则:

转自:http://blog.csdn.net/jationxiaozi/archive/2010/12/24/6096706.aspx

?

?firefox ??image/jpeg ?image/bmp? ? image/gif? ? image/png?ie 6?image/pjpeg? ?image/bmp? image/gif? image/x-png?ie 7?image/pjpeg?image/bmp? image/gif? image/x-png?ie 8?image/pjpeg?image/bmp? image/gif? image/x-png

?

?

?

读书人网 >软件架构设计

热点推荐