Sr,我是 derby 新手,当我尝试测试在数据库中显示 Blob 字段时,显示“org.apache.derby.client.am.CloseFilterInputStream”。
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
for (Images o : IM.getAllImages()) {
InputStream is = o.getImg();
byte[] imageBytes = getBytes(is);
out.write(imageBytes);
response.setContentLength(imageBytes.length);
response.getOutputStream().write(imageBytes);
out.close();
}
此外,当我尝试此代码时,出现此错误“java.io.IOException:对象已关闭。”。我错过了什么吗? error
这是将InputStream转换为字节数组的方法
public static byte[] getBytes(InputStream is) throws IOException {
int len;
int size = 1024;
byte[] buf;
if (is instanceof ByteArrayInputStream) {
size = is.available();
buf = new byte[size];
len = is.read(buf, 0, size);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1) {
bos.write(buf, 0, len);
}
buf = bos.toByteArray();
}
return buf;
}
我的doGet DisplayImagesController.java
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
response.setContentType("image/jpeg");
for (Images o : IM.getAllImages()) {
InputStream is = o.getImg();
byte[] imageBytes = getBytes(is);
out.write(imageBytes);
response.setContentLength(imageBytes.length);
response.getOutputStream().write(imageBytes);
}
out.close();
}
和显示
<table width="70%" border="1">
<tr>
<th>id</th>
<th>IMG</th>
<th>IMG Name</th>
<th>UserID</th>
<th colspan="2">Action</th>
</tr>
<c:forEach items="${imgs}" var="img">
<tr>
<td><c:out value="${img.id}"/></td>
<td><c:out value="${img.imgName}"/></td>
<td><img src="DisplayImagesController" width="200" height="100"/></td>
<td><c:out value="${img.userID}"/></td>
</tr>
</c:forEach>
</table>
但是图像不显示
最佳答案
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
for (Images o : IM.getAllImages()) {
InputStream is = o.getImg();
byte[] imageBytes = getBytes(is);
out.write(imageBytes);
response.setContentLength(imageBytes.length);
response.getOutputStream().write(imageBytes);
}
out.close();
我认为 out.close();
应该位于 for-each
循环之外,因为如果您有多个图像,那么它可能不会创建另一个 out 对象。
关于java - org.apache.derby.client.am.CloseFilterInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40401920/