浏览 4700 次
锁定老帖子 主题:从数据库读图片并在页面中显示
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-28
最后修改:2009-12-28
主要思路: <img src="showImage.do?name=mall&column=image"/>通过src访问servlet,由response的流输出图片到页面。 流的方式一: response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpg"); ServletOutputStream responseOutputStream = response.getOutputStream(); String name = request.getParameter("mall_name"); PreparedStatement ps = null; ResultSet rs = null; Connection con; String sql = "select MALL_LOGO from trustsg_mall_info where MALL_NAME = ?"; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ofbiz","ofbiz","ofbiz"); ps = con.prepareStatement(sql); ps.setString(1, name); rs = ps.executeQuery(); if(rs.next()){ Blob b = rs.getBlob("MALL_LOGO"); Debug.logInfo("showimage"+b, "sssssssssssss"); byte[] images = b.getBytes(1, (int)b.length()); responseOutputStream.write(images); } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } responseOutputStream.flush(); responseOutputStream.close(); 流的方式二: int id= Integer.parseInt(request.getParameter("picid")); String sql = "select pic from p WHERE picid="+id; ResultSet rs=conn.getResult(sql); while(rs.next()) { ServletOutputStream sout = response.getOutputStream(); //图片输出的输出流 InputStream in = rs.getBinaryStream(1); byte b[] = new byte[0x7a120]; for(int i = in.read(b); i != -1;) { sout.write(b); //将缓冲区的输入输出到页面 in.read(b); } sout.flush(); //输入完毕,清除缓冲 sout.close(); 使用jsp: <%@ page contentType="text/html; charset=gbk" %> <%@ page import="java.io.*"%> <%@ page import="java.sql.*, javax.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.math.*"%> <% String photo_no = request.getParameter("photo_no"); //mysql连接 Class.forName("com.mysql.jdbc.Driver").newInstance(); String URL="jdbc:mysql://localhost:3306/job?user=root&password=111111"; Connection con = DriverManager.getConnection(URL); //oracle连接 //String URL="jdbc:oracle:thin@localhost:1521:orcl2"; //user="system"; //password="manager"; //Connection con = DriverManager.getConnection(URL,user,password); try{ // 准备语句执行对象 Statement stmt = con.createStatement(); String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no; ResultSet rs = stmt.executeQuery(sql); if (rs.next()) { Blob b = rs.getBlob("photo_image"); long size = b.length(); //out.print(size); byte[] bs = b.getBytes(1, (int)size); response.setContentType("image/jpeg"); OutputStream outs = response.getOutputStream(); outs.write(bs); outs.flush(); rs.close(); } else { rs.close(); response.sendRedirect("./images/error.gif"); } } finally{ con.close(); } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-12-28
请问lz,在最后的JSP输出图片的例子中,后台是否会有这样的错误? 错误:
java.lang.IllegalStateException
getOutputStream() has already beencalled for this response.
这应该是在JSP中调用getOutputStream()方法时,常见的错误! |
|
返回顶楼 | |
发表时间:2009-12-29
jsp是编辑成servlet利用out来输出页面的的,所以out.Writer()和response.getOutputStream()可能就会有冲突。你可以在使用完输出流以后调用以下两行代码即可: out.clear(); out = pageContext.pushBody(); 试下
|
|
返回顶楼 | |
发表时间:2010-09-13
你好,可以帮忙看下这个问题吗:http://www.iteye.com/problems/50051,谢谢
|
|
返回顶楼 | |