繁体   English   中英

尽管使用了servletContext(),getResourceAsStream仍然返回null

[英]getResourceAsStream returns null inspite of using servletContext()

这是一个读取.pdf并将其作为响应发送的servlet。 我不明白为什么它不起作用。

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”);

这是堆栈跟踪:

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)

你试过sc.getResourceAsStream(“/ WEB-INF / pdf / order.pdf”)吗?

它位于src / main / resources / 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 / webapp / WEB-INF / pdf文件夹中

(而不是src / main / resources ...)。

Vishal,您可以使用我上面的代码并检查文件夹结构再试一次吗?

我在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上下文根的相对路径。 因此,对于上面的代码,您应该将/WEB-INF/pdf/order.pdf的实际路径的insead传递给getResourceAsStream()方法。

sc.getResourceAsStream("/WEB-INF/pdf/order.pdf")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM