如果你在你的数据库中存的Blob类型的图片,你想读取它并显示为图片。你可以先建一个showimage.jsp页面,如果你连接数据库的密码和用户名跟这里不一样,记得要改一下不然就会出错。
<%@ page contentType="text/html;charset=bg2312"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>显示数据库图片测试页</title>
</head>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:denglu","root","root");
Statement stmt=con.createStatement();
String sql=new String();
sql = "select id from picturenews";
ResultSet rs=stmt.executeQuery(sql);
//显示最后一条记录的图片
rs.last();
%>
<table>
<tr><td><img src='testimageout.jsp?id=<%=rs.getInt("id")%>'></td></tr>
</table>
</body>
</html>
在建另一页面读取数据库中图片testimageout.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/todream";
Connection con = DriverManager.getConnection(URL,"root","root");
try{
// 准备语句执行对象
Statement stmt = con.createStatement();
String sql = " SELECT * FROM todream_exhibition WHERE id = "+ photo_no;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
Blob b = rs.getBlob("workimg");
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();
}
%>
转载http://wenwen.soso.com/z/q114766705.htm 如果你想尝试你必须要将数据库建好,并与这一样或将jsp页面改了跟你的一样。
posted on 2010-11-16 13:49
ForMeBlog 阅读(1197)
评论(0) 编辑 收藏 所属分类:
JSP基础类