1. 程式人生 > >apusic應用伺服器下的FileUpload問題——我的一次移植測試經歷

apusic應用伺服器下的FileUpload問題——我的一次移植測試經歷

在一合作伙伴處進行OA系統移植測試,客戶系統採用jsp+javabean開發,部分servlet。有一個公告發布功能,允許上傳附件,在tomcat應用伺服器下執行正常,在apusic伺服器下報空指標異常。

功能涉及到的檔案主要三個:gonggao_add.jsp、gonggao_insert.jsp、
HttpFileUpload.java。其中填寫表單、選擇附件在gonggao_add.jsp,然後提交到
gonggao_insert.jsp,在gonggao_insert.jsp裡用到HttpFileUpload.java。

報錯資訊如下,顯示問題出在HttpFileUpload.java這個類的parseRequest()方法:

2009-09-04 08:52:13 錯誤 [apusic.web.dzzw./dzzw] 執行Servlet時發生錯誤。
java.lang.NullPointerException
at org.apache.commons.fileupload.MultipartStream
$ItemInputStream.makeAvailable(MultipartStream.java:967)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read
(MultipartStream.java:887)
at java.io.InputStream.read(InputStream.java:85)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:94)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64)
at org.apache.commons.fileupload.MultipartStream.readBodyData
(MultipartStream.java:593)
at org.apache.commons.fileupload.MultipartStream.discardBodyData
(MultipartStream.java:619)
at org.apache.commons.fileupload.MultipartStream.skipPreamble
(MultipartStream.java:638)
at com.cnblogs.zxub.upload.HttpFileUpload.parseRequest
(HttpFileUpload.java:212)
at
_jspx._bangong._oa._gonggao._program._gonggao_5finsert__jsp._jspService
(bangong/oa/gonggao/program/gonggao_insert.jsp:29)
at com.apusic.web.jsp.runtime.HttpJspPageImpl.service(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at com.apusic.web.container.ServletComponent.service(Unknown Source)
at com.apusic.web.container.WebContainer.invoke(Unknown Source)
at com.apusic.web.container.WebContainer.invoke(Unknown Source)
at com.apusic.web.jsp.JspServlet.service(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at com.apusic.web.container.ServletComponent.service(Unknown Source)
at com.apusic.web.container.WebContainer.invoke(Unknown Source)
at com.apusic.web.container.WebContainer.processRequest(Unknown
Source)
at com.apusic.web.http.VirtualHost.processRequest(Unknown Source)
at com.apusic.web.http.HttpServer.processRequest(Unknown Source)
at com.apusic.web.http.HttpConnectionHandler.service(Unknown Source)
at com.apusic.web.http.ConnectionHandler.processRequest(Unknown
Source)
at com.apusic.web.http.ConnectionHandler.processConnection(Unknown
Source)
at com.apusic.web.http.ConnectionHandler.run(Unknown Source)
at com.apusic.util.ThreadPoolImpl$WorkerThread.run(Unknown Source)


當時由於不知道的原因,HttpFileUpload.java這個檔案客戶無法提供原始碼,沒辦法進行跟蹤除錯,定位具體出錯的位置;

gonggao_insert.jsp頁面部分程式碼如下:

String filename="";
String newfilename="";

String path="UploadFile/bangong/oa/gonggao";

DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);

factory.setRepository(new File(application.getRealPath("\\") + path));
HttpFileUpload upload = new HttpFileUpload(factory);
upload.setAllowFileTypes("");
upload.setSizeMax(1000*1024*1024);
List fileItems = upload.parseRequest(request); //異常提示在此報錯(gonggao_insert.jsp:29)
Iterator iter = fileItems.iterator();
while(iter.hasNext()){
FileItem fi=(FileItem)iter.next();
String fileName = fi.getName();
File tempFile = new File(fileName);
fileName = tempFile.getName();
String newfileName="";

if(!"".equals(fileName))
{
fileName=fileName.substring(fileName.lastIndexOf("\\")+1,fileName.length());
String Suffix=fileName.substring(fileName.lastIndexOf("."),fileName.length());
uuid.UUID uuid0=new uuid.UUID();
String ID=String.valueOf(uuid0);
newfileName=ID+Suffix;
newfilename+=newfileName+"@@";
filename+=fileName+"@@";
fi.write(new File(application.getRealPath("\\") + path, newfileName));
}
}


經跟蹤gonggao_insert.jsp程式碼發現,檔案中如下程式碼在tomcat和apusic應用伺服器下結果不一致:

application.getRealPath("\\"),但是進行修改後仍然報同樣的錯誤。

在現場測試發現,gonggao_add.jsp頁面中,即便沒有選擇任何附件,提交後都報異常,增加附件後提交也報異常,異常資訊都是一樣的。gonggao_insert.jsp頁面中,把上傳附件相關程式碼註釋掉後,除附件外的其他表單資料可以正常提交儲存。

後來經過跟同事多次溝通,仔細分析了出錯資訊,終於找到了問題原因和解決辦法:刪除%apusic_home%\lib\ext\operamasks-third-party.jar後一切OK!

原因分析:apusic應用伺服器為了支援operamasks框架的上傳元件,在operamasks-third-party.jar內建了apache fileupload相關類檔案,由於apusic應用伺服器預設首先載入自帶的jar包,導致夥伴OA系統lib下的fileupload.jar裡的相關類沒有得到載入,而apusic應用伺服器自帶的fileupload相關類跟夥伴OA系統Lib下版本不一致,導致出現問題。