下载 servlet 的 doGet() 抛出 NullPointerException

发布于 2024-12-27 03:17:26 字数 1094 浏览 0 评论 0原文

我正在开发一个简单的 Web 应用程序,客户端应该能够通过单击 HTML 页面中的超链接来下载 pdf 文件。我正在使用 MVC 模式。下面是我的 Servlet 代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    response.setContentType("application/pdf");
    ServletContext ctx = getServletContext();
    InputStream is = ctx.getResourceAsStream("/abc.pdf");

    int read = 0;
    byte [] bytes = new byte[1024];

    OutputStream os = response.getOutputStream();
    while((read = is.read(bytes)) != -1)
    {
        os.write(bytes, 0, read);
    }
    os.flush();
    os.close();
}

我正在使用 Apache Tomcat 6.0< /strong>

下面是我收到的错误:

SEVERE: Servlet.service() for servlet download threw exception
java.lang.NullPointerException
    at BookDownloaderServlet.doGet(BookDownloaderServlet.java:41)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

abc.pdf 正确放置在应用程序上下文下 。仍然找不到此异常的原因。 请帮忙。 提前致谢

I am developing a simple web application in which client should be able to download a pdf file by clicking a hyperlink from the HTML page.I am using MVC pattern.Below is my code for Servlet :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    response.setContentType("application/pdf");
    ServletContext ctx = getServletContext();
    InputStream is = ctx.getResourceAsStream("/abc.pdf");

    int read = 0;
    byte [] bytes = new byte[1024];

    OutputStream os = response.getOutputStream();
    while((read = is.read(bytes)) != -1)
    {
        os.write(bytes, 0, read);
    }
    os.flush();
    os.close();
}

I am using Apache Tomcat 6.0

Below is the error i am getting :

SEVERE: Servlet.service() for servlet download threw exception
java.lang.NullPointerException
    at BookDownloaderServlet.doGet(BookDownloaderServlet.java:41)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

abc.pdf is correctly placed under application contex . Still cant find a reason for this exception.
kindly help.
thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛泛之交 2025-01-03 03:17:26

这行很可能是您的问题:

InputStream is = ctx.getResourceAsStream("/abc.pdf");

is 被分配给 null。它在这里抛出一个异常:

is.read(bytes)

看起来 CLASSPATH 的根目录中不存在 abc.pdf 文件。

Most likely this line is your problem:

InputStream is = ctx.getResourceAsStream("/abc.pdf");

is is assigned to null. It throws an exception here:

is.read(bytes)

Looks like abc.pdf file does not exist in the root of your CLASSPATH.

高冷爸爸 2025-01-03 03:17:26

尝试将 InputStream is = ctx.getResourceAsStream("/abc.pdf"); 更改为 InputStream is = ctx.getResourceAsStream("abc.pdf");

删除 /

try changing InputStream is = ctx.getResourceAsStream("/abc.pdf"); to InputStream is = ctx.getResourceAsStream("abc.pdf");

remove /

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文