通过JSP显示BLOB(图像) [英] Display BLOB (image) through JSP

查看:127
本文介绍了通过JSP显示BLOB(图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码来显示员工的图表。

I have a code to show a chart o employees.

数据(名称,电话,照片等)存储在SQLServer中并通过JSP显示。
显示数据没问题,但图像.jpg除外(存储在IMAGE = BLOB列中)。

The data (name, phone, photo etc) are stored in SQLServer and displayed through JSP. Showing the data is ok, except the image .jpg (stored in IMAGE=BLOB column).

顺便说一句,我已经有了图像显示(见下面的代码),但我不知道如何把它放在.css中定义的区域(也参见下面的代码),因为通过resultSet的图像被加载到浏览器的整个页面中。

By the way, I've already got the image displayed (see code below), but I dont't know how to put it in the area defined in a .css (see code below, too), since the image got through the resultSet is loaded in the whole page in the browser.

有谁知道我如何'构图'图像?

Does anyone knows how can I 'frame' the image ?

<%
Connection con = FactoryConnection_SQL_SERVER.getConnection("empCHART");
Statement stSuper = con.createStatement();
Statement stSetor = con.createStatement();

Blob image = null;
byte[] imgData = null;

ResultSet rsSuper = stSuper.executeQuery("SELECT * FROM funChart WHERE dept = 'myDept'");

if (rsSuper.next()) {
image = rsSuper.getBlob(12);
imgData = image.getBytes(1, (int) image.length());
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
//o.write(imgData); // even here we got the same as below.
//o.flush();
//o.close();

--[...]

<table style="margin: 0px; margin-top: 15px;">
<tr>
<td id="photo">
<img title="<%=rsSuper.getString("empName").trim()%>" src="<%= o.wite(imageData); o.flush(); o.close(); %>" />
</td>
</td>

<td id="empData">
<h3><%=rsSuper.getString("empName")%></h3>
<p><%=rsSuper.getString("Position")%></p>
<p>Id:<br/><%=rsSuper.getString("id")%></p>
<p>Phone:<br/><%=rsSuper.getString("Phone")%></p>
<p>E-Mail:<br/><%=rsSuper.getString("Email")%></p>
</td>
</table>

以下是应该对图像进行构图的片段:

And here is the fragment supposed to frame the image:

#photo
{
    padding: 0px;
    vertical-align: middle;
    text-align: center;
    width: 170px;
    height: 220px;
}

提前致谢!

推荐答案

你在这里犯了一些根本性的错误。 < img src> 必须指向一个网址,不包含图片的二进制内容。 JSP页面本身的内容类型不应设置为 image / gif 。它应该默认为 text / html 。 Web服务器不应该像您期望的那样在HTML结果中包含具体图像。 Web浏览器根据 src 属性中的URL单独下载图像,然后相应地显示它们。

You're making some fundamental mistakes here. The <img src> must point to an URL, not contain the image's binary content. The content type of the JSP page itself should not be set to image/gif. It should be kept default to text/html. It is not true that the webserver is supposed to include the concrete images in the HTML result as you seemed to expect. It's the webbrowser who downloads the images individually based on the URL found in src attribute and then presents them accordingly.

最简单的方法是创建一个单独的servlet,将图像从DB流式传输到响应主体。您可以通过请求参数或路径信息唯一标识图像。下面是一个使用请求参数的示例:

Easiest is to create a separate servlet which streams the image from the DB to the response body. You can uniquely identify the image by a request parameter or path info. Here's an example which uses a request parameter for that:

<img src="imageServlet?id=<%=rsSuper.getString("id")%>" />

doGet()方法应基本上执行这项工作:

The doGet() method should then basically perform this job:

String id = request.getParameter("id");

// ...

InputStream input = resultSet.getBinaryStream("imageColumnName");
OutputStream output = response.getOutputStream();
response.setContentType("image/gif");
// Now write input to output the usual way.






不相关到具体的问题,使用 scriptlets 这种方式正式强烈劝阻十年。也许您正在阅读完全过时的书籍/教程,或者正在维护一个古老的JSP Web应用程序。有关一些见解,请参阅以下问题的答案以获取一些提示:


Unrelated to the concrete problem, using scriptlets this way is officially strongly discouraged since a decade. Perhaps you were reading completely outdated books/tutorials or are maintaining an ancient JSP web application. For some insights, see also the answers of the following questions for some hints:

  • How to avoid Java code in JSP files?
  • Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
  • How to retrieve and display images from a database in a JSP page?

这篇关于通过JSP显示BLOB(图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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