getResourceAsStream returns null inspite of using servletContext()
13,242
Did you try sc.getResourceAsStream("/WEB-INF/pdf/order.pdf")
and is it located under src/main/resources/WEB-INF/pdf ? file named .pdf and not .PDF?
edit: I tried this at home and it works for me.
try{
is = sc.getResourceAsStream("/WEB-INF/pdf/order.pdf"); // this works!
}catch(Exception e){
e.printStackTrace();
}
Note: give you have a standard build, order.pdf should be in src/main/webapp/WEB-INF/pdf folder
(rather than src/main/resources...).
Vishal, can you give this another try using my code above and checking the folder structure?
![Vishal Anand](https://i.stack.imgur.com/wD7mZ.jpg?s=256&g=1)
Author by
Vishal Anand
Updated on June 04, 2022Comments
-
Vishal Anand about 2 years
This is a servlet that reads a .pdf and sends it as response. I don't understand why is it not working.
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(); }
Can anyone explain why is it not working, ispite of using this.getServletContext().getRealPath("/WEB-INF/pdf/demo.pdf");
here is the stack trace:
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)
-
vikingsteve over 11 yearsVishal, if no-one else answers, I can try out your code when I get home tonight... about 6 hours from now :)
-
Vishal Anand over 11 yearsthis is the path value returned for me as the real path for the file : Y:\eclipse_projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\itext\WEB-INF\pdf\order.pdf. Do you know why is it that? Also I tried your code and it worked fine but as per my knowledge the files in the WEB-INF dir cannot be accessed this way,right? They are protected and should be accessed by realPath() method. Correct me if I am wrong. Thanks for your support :)
-
vikingsteve over 11 yearsNo worries, Vishal. Using real paths exposes your program to the underlying workings of the web container (in this case, Tomcat), and that generally isn't good. Plus you don't even really know if your pdf has been unpacked from the WAR to the filesystem yet. For this reason, it's preferred to use things like
ServletContext.getResourceAsStream
for accessing resources in WARs orClassLoader.getResourceAsStream
for accessing resources in JARs. -
shruti tiwari over 7 yearsIm having a similar problem, though it only appears after the applications been idle for a long time (like over night). Have not found a solution, but will try request.getServletContext instead of the one from "this". Writing here in case I forget to come back and report success/failure