如何从JSP页面中的数据库中检索和显示图像? [英] How to retrieve and display images from a database in a JSP page?

查看:138
本文介绍了如何从JSP页面中的数据库中检索和显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JSP页面中检索和显示数据库中的图像?

How can I retrieve and display images from a database in a JSP page?

推荐答案

让我们分步看看会发生什么:

Let's see in steps what should happen:


  • JSP基本上是一种应该生成HTML输出的视图技术。

  • 要显示HTML中的图片,您需要HTML < img> 元素。

  • 要让它找到图像,您需要指定其 src 属性。

  • src 属性需要指向有效的 http:// URL,因此不是本地磁盘文件系统路径 file:// ,因为当服务器和客户端在物理上不同的机器上运行时,它永远不会工作。

  • 图片网址需要在请求路径中包含图片标识符(例如 http://example.com/context/images/foo.png )或作为请求参数(例如 http://example.com/context/images?id=1 )。

  • 在JSP / Servlet世界中,你可以让Servlet监听某个URL模式,比如 / images / * ,这样你就可以了可以只在特定的URL上执行一些Java代码。

  • 图像是二进制数据,可以 byte [] InputStream 来自数据库, JDBC API 提供 ResultSet#getBytes() ResultSet#getBinaryStream() ,以及 JPA API 提供 @Lob

  • 在Servlet中你可以把这个 byte [] InputStream 写到 OutputStream 通常的反应 Java IO 方式。

  • 客户端需要指示数据应该作为图像处理,至少 还需要设置Content-Type 响应标头。你可以通过 ServletContext#getMimeType() 基于图像文件扩展名,您可以通过< mime-mapping> <扩展和/或覆盖 in web.xml

  • JSP is basically a view technology which is supposed to generate HTML output.
  • To display an image in HTML, you need the HTML <img> element.
  • To let it locate an image, you need to specify its src attribute.
  • The src attribute needs to point to a valid http:// URL and thus not a local disk file system path file:// as that would never work when the server and client run at physically different machines.
  • The image URL needs to have the image identifier in either the request path (e.g. http://example.com/context/images/foo.png) or as request parameter (e.g. http://example.com/context/images?id=1).
  • In JSP/Servlet world, you can let a Servlet listen on a certain URL pattern like /images/*, so that you can just execute some Java code on specific URL's.
  • Images are binary data and are to be obtained as either a byte[] or InputStream from the DB, the JDBC API offers the ResultSet#getBytes() and ResultSet#getBinaryStream() for this, and JPA API offers @Lob for this.
  • In the Servlet you can just write this byte[] or InputStream to the OutputStream of the response the usual Java IO way.
  • The client side needs to be instructed that the data should be handled as an image, thus at least the Content-Type response header needs to be set as well. You can obtain the right one via ServletContext#getMimeType() based on image file extension which you can extend and/or override via <mime-mapping> in web.xml.

那应该是它。它几乎写了代码本身。让我们从HTML开始(在 JSP 中):

That should be it. It almost writes code itself. Let's start with HTML (in JSP):

<img src="${pageContext.request.contextPath}/images/foo.png">
<img src="${pageContext.request.contextPath}/images/bar.png">
<img src="${pageContext.request.contextPath}/images/baz.png">

您可以根据需要动态设置 src 使用 JSTL进行迭代时使用 EL

You can if necessary also dynamically set src with EL while iterating using JSTL:

<c:forEach items="${imagenames}" var="imagename">
    <img src="${pageContext.request.contextPath}/images/${imagename}">
</c:forEach>

然后定义/创建 servlet / images / * 的URL模式上侦听GET请求,下面的示例使用普通的vanilla JDBC来完成这项工作:

Then define/create a servlet which listens on GET requests on URL pattern of /images/*, the below example uses plain vanilla JDBC for the job:

@WebServlet("/images/*")
public class ImageServlet extends HttpServlet {

    // content=blob, name=varchar(255) UNIQUE.
    private static final String SQL_FIND = "SELECT content FROM Image WHERE name = ?";

    @Resource(name="jdbc/yourDB") // For Tomcat, define as <Resource> in context.xml and declare as <resource-ref> in web.xml.
    private DataSource dataSource;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String imageName = request.getPathInfo().substring(1); // Returns "foo.png".

        try (Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_FIND)) {
            statement.setString(1, imageName);

            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    byte[] content = resultSet.getBytes("content");
                    response.setContentType(getServletContext().getMimeType(imageName));
                    response.setContentLength(content.length);
                    response.getOutputStream().write(content);
                } else {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
                }
            }
        } catch (SQLException e) {
            throw new ServletException("Something failed at SQL/DB level.", e);
        }
    }

}

这就是它。如果您担心HEAD和缓存标头并正确响应这些请求,请使用此静态资源servlet的抽象模板

That's it. In case you worry about HEAD and caching headers and properly responding on those requests, use this abstract template for static resource servlet.

  • How should I connect to JDBC database / datasource in a servlet based application?
  • How to upload an image and save it in database?
  • Simplest way to serve static data from outside the application server in a Java web application

这篇关于如何从JSP页面中的数据库中检索和显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆