从数据库中读出图片并显示的示例代码


< !-- -- -- -- -- -- -- -- -- -- -- -- -- --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> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值