從
數據庫中讀出圖片并顯示的示例代碼
< !-- -- -- --
Servlet-- -- -- -- -->
package Photo;
import
Javax.servlet.*;
import
javax.servlet.http.*;
import
java.io.*;
import
java.util.*;
import
java.lang.*;
import
java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class ShowImage extends HttpServlet {
private static final String CONTENT_TYPE = "image/*";
/**
* 定義數據庫連接字符串,JDBC.odbc橋
*/
private String driver_class = "Oracle.jdbc.driver.OracleDriver";
private String connect_string =
"jdbc:oracle:thin:xxw/xxw@192.168.1.50:1521:ORCL";
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
/********************************************
* 定義應用變量
******************************************/
private String SQLString = ""; //定義查詢語句\\r
public String M_EorrMenage = ""; //定義錯誤信息變量
private InputStream in = null; //定義輸入流\\r
private int len = 10 * 1024 * 1024; //定義字符數組長度
//Initialize global variables
public void init() throws ServletException {
/**
* 連接數據庫\\r
*/
try {
Class.forName(driver_class);
} catch (java.lang.ClassNotFoundException e) {
//異常
System.err.println("databean():" + e.getMessage());
}
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
//在數據庫中的照片的ID
int PHOTOID = 0;
/*********************************************
* 接受上文傳遞的圖片ID號
* 上文傳輸文件名稱為photoid
*********************************************/
try {
PHOTOID = Integer.parseInt(request.getParameter("photoid"));
SQLString = "select * from xxw_photo where p_id=" + PHOTOID;
} catch (Exception e) {
e.printStackTrace();
response.setContentType("text/html; charset=gb2312");
M_EorrMenage = "請輸入圖片ID號";
M_EorrMenage =
new String(M_EorrMenage.getBytes("ISO8859_1"), "GBK");
out.println("<%@ page contentType=\'text/html; charset=gb2312\' %>");
out.println("<html>");
out.println("<head><title>id</title></head>");
out.println("<body>");
out.println("<p>" + M_EorrMenage + "</p>");
out.println("</body></html>");
}
/*****************************************************
* 執行查詢語句\\r
*****************************************************/
try {
conn = DriverManager.getConnection(connect_string);
stmt = conn.createStatement();
rs = stmt.executeQuery(SQLString);
} //try
catch (SQLException ex) {
System.err.println("aq.executeUpdate:" + ex.getMessage());
M_EorrMenage = "對不起,數據庫無法完成此操作!";
M_EorrMenage =
new String(M_EorrMenage.getBytes("ISO8859_1"), "GBK");
response.setContentType("text/html; charset=gb2312");
out.println("<html>");
out.println("<head><title>no_database</title></head>");
out.println("<body>");
out.println("<p>" + M_EorrMenage + "</p>");
out.println("</body></html>");
}
/*********************************************
* 將圖片流讀入字符數組中,并顯示到客戶端
********************************************/
try {
if (rs.next()) {
in = rs.getBinaryStream("photo");
response.reset(); //返回在流中被標記過的位置
response.setContentType("image/jpg"); //或gif等
// int len=in.available();//得到文件大小
OutputStream toClient = response.getOutputStream();
byte[] P_Buf = new byte[len];
int i;
while ((i = in.read(P_Buf)) != -1) {
toClient.write(P_Buf, 0, i);
}
in.close();
toClient.flush(); //強制清出緩沖區\\r
toClient.close();
} else {
M_EorrMenage = "無此圖片!";
M_EorrMenage =
new String(M_EorrMenage.getBytes("ISO8859_1"), "GBK");
response.setContentType("text/html; charset=gb2312");
out.println("<html>");
out.println(
"<head><title>this photo isn\'t have</title></head>");
out.println("<body>");
out.println("<p>" + M_EorrMenage + "</p>");
out.println("</body></html>");
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
M_EorrMenage = "無法讀取圖片!";
M_EorrMenage =
new String(M_EorrMenage.getBytes("ISO8859_1"), "GBK");
response.setContentType("text/html; charset=gb2312");
out.println("<%@ page contentType=\'text/html; charset=gb2312\' %>");
out.println("<html>");
out.println("<head><title>no photo</title></head>");
out.println("<body>");
out.println("<p>" + M_EorrMenage + "</p>");
out.println("</body></html>");
}
}
//Clean up resources
public void destroy() {
try {
conn.close();
} catch (SQLException e) {
System.err.println("aq.executeUpdate:" + e.getMessage());
M_EorrMenage = "對不起,數據庫無法完成此操作!";
}
}
}
<!---------------------------顯示---------------------------------------------->
<html>
<head>
<title>Untitled Document</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table>
<%
int i=1;
while(i<3){
%>
<tr>
<td colspan="3"> <img border="1" src="http://192.168.1.50:8100/ShowImage?photoid=<%=i%>"></td>
</tr>
<%
i++;
}
%>
</table>
</body>
</html>
posted on 2006-11-21 19:54
選寶網an9 閱讀(624)
評論(0) 編輯 收藏