从数据库读图片并在页面中显示
主要思路:
<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();
}
163编辑器我是没用过,但可以共同学习讨论。我用的比较多的是tinyMCE,你可以email我,我们一起讨论。 我的email:dfhstudio@163.com
嗯,我在学习163编辑器,遇到很多问题了,你能帮忙解决吗?! :D
原理都是相同的,第二种方法中rs.getBinaryStream(1);是取数据库中图片的二进制,你要确定,在数据库中存的就是图片的二进制形式
我用的是第二个,出问题了,一大片乱码
你好,可以帮忙看下这个问题吗:http://www.iteye.com/problems/50051,谢谢
jsp是编辑成servlet利用out来输出页面的的,所以out.Writer()和response.getOutputStream()可能就会有冲突。你可以在使用完输出流以后调用以下两行代码即可: out.clear(); out = pageContext.pushBody(); 试下