jfy3d(剑事)BLOG

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  37 随笔 :: 0 文章 :: 363 评论 :: 0 Trackbacks

webwork里上传文件比较方便,几行代码就可以完成。
只是有个小问题,就是当form里的file控件没有选文件的时提交action
就会出现异常,通常很多应用中不一定非要带文件上传的,这个应该
算webwork一个bug吧。

从报错的地方可以查出是在fileupload拦截器中
String[] fileName = multiWrapper.getFileNames(inputName);
这一行开始中断掉的
就是multiWrapper.getFileNames(inputName);中出现空指针错误

如果webwork.properties中配置的是pell上传包
需要找到PellMultiPartRequest这个类的以下方法
//--------------------------------------------------------------------------------------
public String[] getFileNames(String fieldName) {
              // TODO - not sure about this - is this the filename of the actual file or
              // TODO - the uploaded filename as provided by the browser?
              // TODO - Not sure what version of Pell this class uses as it doesn't seem to be the latest 
              //这里倒是说明了条件,就是不知道为什么还这么做
            return new String[]{multi.getFile(fieldName).getName()};
}
---------------------------------------------------------------------------------------//



可以看到
multi.getFile(fieldName).getName()如果文件为空肯定会报错的
可以先修改成以下方式
//---------------------------------------------------------------------------------------
public String[] getFileNames(String fieldName) {
        if(multi.getFile(fieldName)!=null)   //多加一个判断
            return new String[]{multi.getFile(fieldName).getName()};
        else
            return new String[]{};
    }
----------------------------------------------------------------------------------------//
这样form里file不选文件就不在出异常了
用cos上传处理方法一样



除上面方法
还修改fileupload拦截器也可以解决
String[] fileName = multiWrapper.getFileNames(inputName);
找到这行
然后修改成下面样子
//-----------------------------------------------------------------------------------
String[] fileName =null;

try{
      fileName = multiWrapper.getFileNames(inputName);
}catch(Exception ex){
      //file is null
}
-----------------------------------------------------------------------------------//

posted on 2006-02-20 10:15 剑事 阅读(2183) 评论(0)  编辑  收藏 所属分类: webwork

只有注册用户登录后才能发表评论。


网站导航: