在数据库中存储为 BLOB 的 img 标签中显示图像 [英] Display image in img tag stored as BLOB in database

查看:277
本文介绍了在数据库中存储为 BLOB 的 img 标签中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法显示保存在 mysql 中的 BLOB 图像.我有豆子,jsp.我使用3multy-tier架构,我想把所有的产品都用图片展示.

I can't display BLOB image that I have in mysql saved. I have beans, jsp. I use 3multy-tier architecture, I want to display all the products with the picture.

在附件中:

 try {
        Connection cn = getVla().getConnection();
        String sql = "SELECT * FROM products";
        PreparedStatement pst = cn.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        ArrayList<products> ls = new ArrayList<products>();
        while(rs.next()) {
            products s = new products();
            s.setPk(rs.getLong("pk"));
            s.setName(rs.getString("name"));
            s.setPrice(rs.getDouble("price"));
            s.setPic(rs.getBlob("pic"));
            s.setComments(rs.getString("comments"));
            ls.add(s);
        }
        return ls;
    }

在产品中:

 public Blob getPic() {
        return pic;
    }

在 main.jsp

<%=
List<products> product = bean.getproducts();

%>
<h1>Product: </h1>
<%  
for(products c : product) { 
%>
From <%= c.getName()%> <br/>
<%= c.getPic()%></b><br/>
<b><%= c.getPrice()%> </b><br/>
<%= c.getComments()%>
<hr/>
<%
}
%>

如何显示图片?(目前我正在显示 com.mysql.jdbc.Blob@2e5f6a64)

How I can display the picture? (Currently I am getting com.mysql.jdbc.Blob@2e5f6a64 in display)

推荐答案

你看到的是 Blob.toString() 的结果.由于它是二进制内容,JVM 无法真正找到一个很好的表示.

What you're seeing is the result of Blob.toString(). Since it's binary content, the JVM cannot really figure a nice representation.

您应该做的是创建一个单独的 Servlet,它只从数据库中检索 Blob 并将其内容流式传输到 response.getOutputStream().在您的 JSP 中,您添加一个 <img> 标记,其 src-attribute 指向您刚刚编写的 Servlet.

What you should do is create a seperate Servlet that only retrieves the Blob from the database and streams its content to response.getOutputStream(). In your JSP, you add an <img> tag whose src-attribute points to the Servlet you just wrote.

Servlet 应该一次读取一个产品的图像,因此查询会略有不同:应该足够了

The Servlet should read the image for one product at a time, so the query would be slightly different: it should be enough to have

String sql = "SELECT pic FROM products where pk = " + pk;

请注意,您需要使用某些请求参数指定此 pk 变量.上面这行代码只是演示这个想法的一个例子.将请求 URL 逐字复制到 SQL 查询中非常不安全.谷歌搜索SQL 注入"以了解更多相关信息.

Note that you need to specify this pk variable using some request parameter. The above line of code is just an example to demonstrate the idea. It is very unsafe to literally copy a request URL into an SQL query. Google for "SQL Injection" to read more about that.

使用 Blob.getInputStream() 你可以获得一个 InputStream 的内容你可以复制到 response.getOutputStream() 以便写回浏览器.不要忘记在该响应上设置适当的 content-type,例如在 JPEG 图片的情况下为image/jpg".

Using Blob.getInputStream() you can obtain an InputStream whose contents you could copy to response.getOutputStream() in order to write it back to the browser. Don't forget to set the appropriate content-type on that response, for example "image/jpg" in case of JPEG pictures.

这篇关于在数据库中存储为 BLOB 的 img 标签中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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