2013-02-21 181 views
1

這是一個讀取.pdf並將其作爲響應發送的servlet。我不明白爲什麼它不起作用。getResourceAsStream返回null,儘管使用servletContext()

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     response.setContentType("application/pdf"); 
     ServletContext sc = this.getServletContext(); 
     String path = sc.getRealPath("/WEB-INF/pdf/order.pdf"); 
     System.out.print(path);// this prints correct path 
     InputStream is = null; 
     try{ 
      is = sc.getResourceAsStream(path); // this is not working 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     System.out.print(is);// it is null 
     int read = 0; 
     byte[] bytes = new byte[2048]; 
     OutputStream os = response.getOutputStream(); 

     try{ 
     while((read = is.read(bytes))!= -1){ //exception is thrown here 
      os.write(bytes,0,read); 
     } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     os.flush(); 
     os.close(); 
     } 

誰能解釋爲什麼它不工作使用this.getServletContext()getRealPath( 「/ WEB-INF/PDF/demo.pdf」)的ispite,。

這裏是堆棧跟蹤:

2/21/13 14:24:21:322 IST] 00000033 SystemErr  R java.lang.NullPointerException 
[2/21/13 14:24:21:323 IST] 00000033 SystemErr  R  at com.tgmc.servlets.DisplayOrder_PDF_Servlet.doGet(DisplayOrder_PDF_Servlet.java:42) 
[2/21/13 14:24:21:323 IST] 00000033 SystemErr  R  at javax.servlet.http.HttpServlet.service(HttpServlet.java:718) 
[2/21/13 14:24:21:323 IST] 00000033 SystemErr  R  at javax.servlet.http.HttpServlet.service(HttpServlet.java:831) 
[2/21/13 14:24:21:323 IST] 00000033 SystemErr  R  at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1449) 
[2/21/13 14:24:21:323 IST] 00000033 SystemErr  R  at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:790) 
[2/21/13 14:24:21:323 IST] 00000033 SystemErr  R  at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:443) 
[2/21/13 14:24:21:323 IST] 00000033 SystemErr  R  at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:175) 
[2/21/13 14:24:21:323 IST] 00000033 SystemErr  R  at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3610) 
[2/21/13 14:24:21:323 IST] 00000033 SystemErr  R  at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:274) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:926) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1557) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:173) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:455) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:384) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:288) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.ssl.channel.impl.SSLConnectionLink.determineNextChannel(SSLConnectionLink.java:1016) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.ssl.channel.impl.SSLConnectionLink$MyReadCompletedCallback.complete(SSLConnectionLink.java:639) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.ssl.channel.impl.SSLReadServiceContext$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1772) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165) 
[2/21/13 14:24:21:324 IST] 00000033 SystemErr  R  at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217) 
[2/21/13 14:24:21:325 IST] 00000033 SystemErr  R  at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161) 
[2/21/13 14:24:21:325 IST] 00000033 SystemErr  R  at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138) 
[2/21/13 14:24:21:325 IST] 00000033 SystemErr  R  at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:202) 
[2/21/13 14:24:21:325 IST] 00000033 SystemErr  R  at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:766) 
[2/21/13 14:24:21:325 IST] 00000033 SystemErr  R  at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:896) 
[2/21/13 14:24:21:325 IST] 00000033 SystemErr  R  at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1527) 
+0

是您的WAR文件擴展? sc.getRealPath( 「/ WEB-INF/PDF/order.pdf」);這隻有在你的WAR文件被擴展後才能工作 – 2013-02-21 08:25:57

+0

顯示你的堆棧跟蹤,你得到的異常是什麼? – 2013-02-21 08:28:00

+0

@dj aqeel:NullPointerException – 2013-02-21 08:29:44

回答

3

你嘗試sc.getResourceAsStream( 「/ WEB-INF/PDF/order.pdf」)

,並位於其下的src/main /資源/ WEB-INF/pdf?文件名爲.pdf而不是.PDF?

編輯:我在家裏試過這個,它適合我。

try{ 
     is = sc.getResourceAsStream("/WEB-INF/pdf/order.pdf"); // this works! 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

注:給你有一個標準的建立,order.pdf應該在的src/main/web應用/WEB-INF/PDF文件夾

(而不是的src/main /資源.. )。

Vishal,你可以再次嘗試使用我的代碼,並檢查文件夾結構嗎?

+0

是的,我已經嘗試過,但它不工作:( – 2013-02-21 08:32:10

+1

維沙爾,如果沒有其他人回答,我可以嘗試你的代碼,當我今晚回家...大約6小時後:) – vikingsteve 2013-02-21 12:52:31

+0

請參閱我的編輯。 – vikingsteve 2013-02-21 20:05:17

0

我在調用ServletContext上的GetResourceAsStream()時也有NullPointerException,並且已經修復,所以我在這裏爲那些遇到同樣問題的人發帖。根據 http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html的文檔,getRealPath()方法將返回服務器文件系統上的實際路徑,因此,如上面的代碼調用sc.getRealPath("/WEB-INF/pdf/order.pdf")可能會返回/opt/apache-tomcat/wtpwebap‌​ps/itext/WEB-INF/pdf/order.pdf,假定/ opt/apache-tomcat /是您的tomcat的位置服務器。該文件還提到,對於方法getResourceAsStream(),我們應該將相對路徑傳遞給以/開頭的servlet上下文根。因此,對於上面的代碼,您應該將實際路徑的insead傳遞給getResourceAsStream()方法。

sc.getResourceAsStream("/WEB-INF/pdf/order.pdf") 
相關問題