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

styjava_jiaozi 2011-10-18 09:21:01
我使用了DAO,Servlet,javabean,怎么将图片显示出来?
...全文
577 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
态度071 2012-12-25
  • 打赏
  • 举报
回复
这个可以的,你不妨试试。 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; }
styjava_jiaozi 2011-10-19
  • 打赏
  • 举报
回复
解决了,不是用的楼上的方法,原来我是想将图片放到容器中在网页上直接读,但没成功。现我将数据库的流传到jsp上,在网页上转成图片



虽然你的方法没有用到,但还是结贴吧
keketian521 2011-10-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 styjava_jiaozi 的回复:]
能说的具体点
[/Quote]

好东西 分享一下!
阳明 to life 2011-10-18
  • 打赏
  • 举报
回复
styjava_jiaozi 2011-10-18
  • 打赏
  • 举报
回复
能说的具体点
阳明 to life 2011-10-18
  • 打赏
  • 举报
回复
base64

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧