且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 BufferedImage 转换为图像以在 JSP 上显示

更新时间:2023-11-11 21:24:28

首先,JSP 是一种视图技术,它提供了一个模板来编写 HTML/CSS/JS 以及与后端 Java 代码交互以控制页面流和访问的能力后端数据.您的问题更多是在 HTML 中.

现在,要在 HTML 页面中显示图像,您需要 HTML 元素.要定义/分配图像,您只需让 src 属性指向一个 URL.例如

(它可以是相对于当前上下文,也可以是绝对 URL,例如以 http:// 开头)

如果图像是动态的,就像您的情况一样,您需要有一个 Servlet 来侦听与图像 URL 匹配的 url-pattern.例如

(此处 servlet 显然要映射到 /imageservlet/* 的 URL 模式上,图像标识符(此处为文件名)可通过 request.getPathInfo())

将触发 GET 请求,因此您只需实现 servlet 的 doGet() 方法.要发送 HTTP 响应,您需要做的就是将一些内容写入响应的 OutputStream,以及一组表示内容的响应标头 (Content-TypeContent-Length 和/或 Content-disposition).您可以使用

  • 最简单在 Java Web 应用程序中从应用服务器外部提供静态数据的方法
  • I would like to convert BufferedImage to an image that will display on JSP page. How can I achieve this?

    First, JSP is a view technology providing a template to write HTML/CSS/JS in and the ability to interact with backend Java code to control page flow and access backend data. Your problem is more in HTML.

    Now, to display an image in a HTML page, you need the HTML <img> element. To define/allocate an image, you just have to let the src attribute point to an URL. E.g.

    <img src="url/to/image.jpg" />
    

    (it can be either relative to the current context, or an absolute URL, e.g. starting with http://)

    If the image is dynamic, as in your case, you need to have a Servlet which listens on the url-pattern matching the image URL. E.g.

    <img src="imageservlet/image.jpg" />
    

    (here the servlet is obviously to be mapped on an URL pattern of /imageservlet/* and the image identifier, here the filename, is here available by request.getPathInfo())

    The <img src> will fire a GET request, so you just have to implement doGet() method of the servlet. To send a HTTP response all you need to do is to write some content to the OutputStream of the response, along with a set of response headers representing the content (Content-Type, Content-Length and/or Content-disposition). You can use ImageIO#write() to write a BufferedImage to an OutputStream.

    You can find a basic example of such an image servlet here. You just have to replace Files#copy() with ImageIO#write().

    response.setContentType("image/png");
    ImageIO.write(bufferedImage, "png", response.getOutputStream());
    


    As a completely different alternative, you can also let the servlet convert the image to a Base64 encoded string and pass it on to the JSP:

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "png", output);
    String imageAsBase64 = Base64.getEncoder().encodeToString(output.toByteArray());
    request.setAttribute("imageAsBase64", imageAsBase64);
    request.getRequestDispatcher("/WEB-INF/some.jsp").forward(request, response);
    

    And finally show it in the forwarded JSP using the data URI scheme as below:

    <img src="data:image/png;base64,${imageAsBase64}" />
    

    You only need to keep in mind that this doesn't give the server nor the client the opportunity to cache the image. So this approach is plain inefficient in case the image is not temporary.


    See also: