先建立資料庫 如: create table bcctphoto( photoid int primary key auto_increment, photoname varchar(50) NOT NULL, photo blob ); 2把show.jsp放在tomcat的任意目錄下. show.jsp作用:從資料庫中讀出blob,并産生image/jpg. show.jsp檔案如下: %@ page c
先建立資料庫
如:
create table bcctphoto(
photoid int primary key auto_increment,
photoname varchar(50) NOT NULL,
photo blob
);
<2>把show.jsp放在tomcat的任意目錄下. show.jsp作用:從資料庫中讀出blob,并産生image/jpg.
show.jsp檔案如下:
String photo_no = request.getParameter("photo_no");
//mysql連接配接
Class.forName("com.mysql.jdbc.Driver").newInstance();
String URL="jdbc:mysql://localhost:3306/bigdate?user=root&password=mysqladmin";
Connection con = DriverManager.getConnection(URL);
//oracle連接配接
//String URL="jdbc:oracle:[email protected]: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();
}
%>
<3>把如下檔案放在show.jsp的同一目錄下.
index.html檔案如下:
圖像測試
圖像測試
異常處理: 如果出現 getOutputStream() has already been called for this response
異常解析:這裡是在釋放在jsp中使用的對象,會調用response.getWriter(),因為這個方法是和response.getOutputStream()相沖突的!是以會出現以上這個異常。然後當然是要提出解決的辦法,其實挺簡單的,在使用完輸出流以後調用以下兩行代碼即可:
解決方法:添加代碼
out.clear();
out = pageContext.pushBody();
如下代碼:
//注意看以下幾句的使用
outs.flush();
outs.close();
outs=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
本文原創釋出php中文網,轉載請注明出處,感謝您的尊重!