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 劍事 閱讀(2207) 評論(0)  編輯  收藏 所屬分類: webwork
          主站蜘蛛池模板: 南阳市| 开鲁县| 新巴尔虎右旗| 桓仁| 瑞安市| 屯留县| 全椒县| 万盛区| 土默特右旗| 马关县| 迭部县| 平凉市| 阿坝县| 安福县| 体育| 织金县| 河津市| 贵州省| 平度市| 屏东县| 惠安县| 康定县| 桃园县| 黄梅县| 达州市| 东乡| 阿荣旗| 伊川县| 安仁县| 巴南区| 镇康县| 淳化县| 山阳县| 定边县| 什邡市| 汉川市| 庆元县| 焉耆| 铜陵市| 海盐县| 大姚县|