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

查看:9
本文介绍了通过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;
}

提前致谢!

推荐答案

您在这里犯了一些基本错误. 必须指向一个 URL,不包含图像的二进制内容.JSP 页面本身的内容类型不应设置为image/gif.它应该保持默认为 text/html.正如您所期望的那样,Web 服务器不应该在 HTML 结果中包含具体图像.网络浏览器根据在 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,它将图像从数据库流式传输到响应正文.您可以通过请求参数或路径信息来唯一标识图像.这是一个使用请求参数的示例:

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:

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

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