在 servlet 中从文件系统提供静态图像文件?

发布于 2024-10-16 08:06:03 字数 33 浏览 2 评论 0原文

如何通过 servlet 在文件系统中提供图像文件?

How do I serve an image file in the filesystem from a servlet?

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

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

发布评论

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

评论(2

涙—继续流 2024-10-23 08:06:03

看一下这里:示例 Depot:在 Servlet 中返回图像< /a> 链接已损坏。 Wayback Machine 副本插入如下:

// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Get the absolute path of the image
    ServletContext sc = getServletContext();
    String filename = sc.getRealPath("image.gif");

    // Get the MIME type of the image
    String mimeType = sc.getMimeType(filename);
    if (mimeType == null) {
        sc.log("Could not get MIME type of "+filename);
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }

    // Set content type
    resp.setContentType(mimeType);

    // Set content size
    File file = new File(filename);
    resp.setContentLength((int)file.length());

    // Open the file and output streams
    FileInputStream in = new FileInputStream(file);
    OutputStream out = resp.getOutputStream();

    // Copy the contents of the file to the output stream
    byte[] buf = new byte[1024];
    int count = 0;
    while ((count = in.read(buf)) >= 0) {
        out.write(buf, 0, count);
    }
    in.close();
    out.close();
}

Have a look over here: Example Depot: Returning an Image in a Servlet Link broken. Wayback Machine copy inserted below:

// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Get the absolute path of the image
    ServletContext sc = getServletContext();
    String filename = sc.getRealPath("image.gif");

    // Get the MIME type of the image
    String mimeType = sc.getMimeType(filename);
    if (mimeType == null) {
        sc.log("Could not get MIME type of "+filename);
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }

    // Set content type
    resp.setContentType(mimeType);

    // Set content size
    File file = new File(filename);
    resp.setContentLength((int)file.length());

    // Open the file and output streams
    FileInputStream in = new FileInputStream(file);
    OutputStream out = resp.getOutputStream();

    // Copy the contents of the file to the output stream
    byte[] buf = new byte[1024];
    int count = 0;
    while ((count = in.read(buf)) >= 0) {
        out.write(buf, 0, count);
    }
    in.close();
    out.close();
}
作死小能手 2024-10-23 08:06:03

遗憾的是 servlet 规范没有明确的方法来做到这一点,除非图像位于 webapp 目录下。 Servlet 容器通常也不建议其专有方法来执行此操作。显然容器必须这样做才能提供文件,为什么它不公开该功能?为什么不是 HttpServletResponse.sendFile(File)

您最好的选择是创建符号链接,以便您的文件显示在 webapp 目录下。

Well it's kind of a shame that servlet spec doesn't have a clear way to do it, unless the image is located under the webapp dir. Servlet containers do not usually advise their proprietary ways to do this either. Obviously a container must do this to serve files, why doesn't it expose the functionality? Why not a HttpServletResponse.sendFile(File)?

Your best bet is to create symlinks so your files appears be to under webapp dir.

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