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" class="picAction" 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@91e0a8
2011-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" class="picAction" 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

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值