从数据库中读出图片并显示的示例代码
< !-- -- -- --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>
注:此程序对于从数据库读取图片后写入文件请参考代码者留意
|