java web 怎么将数据库中的二进制流图片还原显示在jsp中

public String displayImage(){
String sql = "SELECT empHeadImg,empHeadImgName FROM t_personal WHERE id= " + employeeId;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
try {
Class.forName(driverClassName);
Connection conn = DriverManager.getConnection(url, username,password);
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("empHeadImg");
is = blob.getBinaryStream();
HttpServletResponse response = this.getResponse();
response.setContentType("image/jpeg");
os = response.getOutputStream();
int num = (int) blob.length();
byte buf[] = new byte[num];
while ((num = is.read(buf)) != -1) {
os.write(buf);
}
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}

猜你喜欢

转载自z-408.iteye.com/blog/1844436