gpt4 book ai didi

java - 通过JSP显示BLOB(图像)

转载 作者:行者123 更新时间:2023-11-29 19:03:01 27 4
gpt4 key购买 nike

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

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

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

有人知道如何“框定”图像吗?

<%
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>

这是用于构建图像的片段:

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

提前致谢!

最佳答案

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

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

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

doGet()然后方法应该基本上执行此工作:

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.
<小时/>

与具体问题无关,十年来官方强烈建议不要使用这种方式使用scriptlet。也许您正在阅读完全过时的书籍/教程,或者正在维护一个古老的 JSP Web 应用程序。对于一些见解,另请参阅以下问题的答案以获得一些提示:

关于java - 通过JSP显示BLOB(图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43715137/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com