中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 基础 > Java语言基础
从数据库中读出图片并显示的示例代码
作者:佚名 时间:2007-06-13 13:49 出处:ccidnet.com 责编:月夜寒箫
              摘要:从数据库中读出图片并显示的示例代码
CODE:
--------------------------------------------------------------------------------
< !-- -- -- -- -- -- -- -- -- -- -- -- -- --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 = ""; //定义查询语句
  public String M_EorrMenage = ""; //定义错误信息变量
  private InputStream in = null; //定义输入流
  private int len = 10 * 1024 * 1024; //定义字符数组长度

  //Initialize global variables
  public void init() throws ServletException {
    /**
    * 连接数据库
    */
    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>");

    }
    /*****************************************************
    * 执行查询语句
    *****************************************************/
    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(); //强制清出缓冲区
          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>

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有