sqlserver2000 数据库,image类型的字段
我想从数据库中读取这个图片,然后显示在jsp页面上
在servlet中应该怎么写,jsp中又怎么写呢?
读取流中,然后再设定页面输出形式文件操作流 先从数据库中读出来
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ByteArrayOutputStream localBuffer = new ByteArrayOutputStream();
BufferedOutputStream servletOutput = new BufferedOutputStream(response.getOutputStream());
try {
ResultSet rs = getResultSet();
try {
if (rs.next()) {
BufferedInputStream imageData = new BufferedInputStream(rs.getBinaryStream(1));
int len = 0;
byte[] buffer = new byte[1024 * 1024];
while ((len = imageData.read(buffer)) >= 0) {
localBuffer.write(buffer, 0, len);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
shutdownResultSet(rs);
}
byte[] imageBytes = localBuffer.toByteArray();
response.setContentType(getMimeType(request));
response.setContentLength(imageBytes.length);
response.setHeader("Content-disposition", "inline; filename=\"" + getImageName(request) + "\"");
servletOutput.write(imageBytes);
} finally {
servletOutput.flush();
close(servletOutput);
close(localBuffer);
}
}
然后在写入页面