在一合作伙伴处进行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(UnknownSource) 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(UnknownSource) at com.apusic.web.http.ConnectionHandler.processConnection(UnknownSource) 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下版本不一致,导致出现问题。