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

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

问题描述

我无法显示mysql中保存的BLOB图像.我有豆子,jsp.我使用3multy层体系结构,我希望将所有产品与图片一起显示.

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,该Servlet仅从数据库中检索Blob,并将其内容流式传输到response.getOutputStream().在您的JSP中,添加一个<img>标记,该标记的src属性指向您刚刚编写的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查询中是非常不安全的. Google可以通过"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天全站免登陆