webwork里上傳文件比較方便,幾行代碼就可以完成。
只是有個小問題,就是當form里的file控件沒有選文件的時提交action
就會出現(xiàn)異常,通常很多應(yīng)用中不一定非要帶文件上傳的,這個應(yīng)該
算webwork一個bug吧。
從報錯的地方可以查出是在fileupload攔截器中
String[] fileName = multiWrapper.getFileNames(inputName);
這一行開始中斷掉的
就是multiWrapper.getFileNames(inputName);中出現(xiàn)空指針錯誤
如果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
}
-----------------------------------------------------------------------------------//
只有注冊用戶登錄后才能發(fā)表評論。 | ||
![]() |
||
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
|
||
相關(guān)文章:
|
||