在JSP中从数据库中检索图像(但我无法在特定位置检索图像)

时间:2011-07-23 06:02:22

标签: image jsp

  

可能重复:
  How to retrieve and display images from a database in a JSP page?

我想在表格的<td>标签中显示图片。代码工作正常,但我无法在表的<td>标记中检索图像

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*,java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>

    <body>
        <%Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:IMG");
        Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs=st.executeQuery("select image from img");

        %>
        <table width="100%" border="2">
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <%
            while(rs.next()) {
            byte[] bytearray = new byte[1048576];
            int size=0;
            InputStream sImage = rs.getBinaryStream(1);

            response.reset();
            response.setContentType("image/jpeg");
            while((size=sImage.read(bytearray))!= -1 ){
            %>
            <tr>
                <td>&nbsp;</td>
                <td><img src="<%= response.getOutputStream().write(bytearray,0,size)%>"width=50 height=50 /></td>
                <td>&nbsp;</td>
            </tr>
            <%
            }
            }     
            %>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

在HTML(jsp)页面中设置不同的内容类型内部不起作用。在JSP内部,您必须生成指向图像的链接。浏览器将在单独的请求中请求图像,为此您需要类似servlet的东西:

<强> 1。步骤:生成JSP

    <%Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:IMG");
    Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

    /* you need an identifier for each image, like an ID */
    ResultSet rs=st.executeQuery("select name, image from img");

    %>
    <table width="100%" border="2">
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <%
        while(rs.next()) {
        %>
        <tr>
            <td>&nbsp;</td>
            <td><img src="${path_to_your_image_serving_resource}/<%=rs.getString("name")%>"width=50 height=50 /></td>
            <td>&nbsp;</td>
        </tr>
        <%
        }     
        %>
    </table>

<强> 2。步骤:创建一个servlet

您的servlet必须收听${path_to_your_image_serving_resource}/*。如果浏览器请求此URL,则必须根据其名称检索图像,设置响应的coorect内容类型,并将字节流回浏览器。

请记住,在HTML / HTTP中,图像是一个独立的资源。

BTW:你应该仔细看看你的设计:

  • 您不使用连接池,而是使用DriverManager并根据请求创建与数据库的新连接。这既不会很好,也不会扩展。看javax.sql.DataSource
  • 直接在JSP中定义SQL(通常)是一种不好的做法