如果你在你的數據庫中存的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 閱讀(1213)
評論(0) 編輯 收藏 所屬分類:
JSP基礎類