小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

從數(shù)據(jù)庫(kù)中讀出圖片并顯示的示例代碼

 高山流水 2005-10-26





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/*";
    /**
    * 定義數(shù)據(jù)庫(kù)連接字符串,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;
    /********************************************
    * 定義應(yīng)用變量
    ******************************************/
    private String SQLString = ""; //定義查詢(xún)語(yǔ)句
    public String M_EorrMenage = ""; //定義錯(cuò)誤信息變量
    private InputStream in = null; //定義輸入流
    private int len = 10 * 1024 * 1024; //定義字符數(shù)組長(zhǎng)度

    //Initialize global variables
    public void init() throws ServletException {
        /**
        * 連接數(shù)據(jù)庫(kù)
        */
        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();
        //在數(shù)據(jù)庫(kù)中的照片的ID
        int PHOTOID = 0;
        /*********************************************
        * 接受上文傳遞的圖片ID號(hào)
        * 上文傳輸文件名稱(chēng)為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 = "請(qǐng)輸入圖片ID號(hào)";
            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>");

        }
        /*****************************************************
        * 執(zhí)行查詢(xún)語(yǔ)句
        *****************************************************/
        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 = "對(duì)不起,數(shù)據(jù)庫(kù)無(wú)法完成此操作!";
            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>");

        }
        /*********************************************
        * 將圖片流讀入字符數(shù)組中,并顯示到客戶(hù)端
        ********************************************/
        try {
            if (rs.next()) {
                in = rs.getBinaryStream("photo");
                response.reset(); //返回在流中被標(biāo)記過(guò)的位置
                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(); //強(qiáng)制清出緩沖區(qū)
                toClient.close();
            } else {
                M_EorrMenage = "無(wú)此圖片!";
                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 = "無(wú)法讀取圖片!";
            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 = "對(duì)不起,數(shù)據(jù)庫(kù)無(wú)法完成此操作!";
        }
    }
}



<!---------------------------顯示---------------------------------------------->
<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>

注:此程序?qū)τ趶臄?shù)據(jù)庫(kù)讀取圖片后寫(xiě)入文件請(qǐng)參考代碼者留意
 
整理發(fā)布:獨(dú)孤求勝 umbrella

文章回復(fù):

Re:從數(shù)據(jù)庫(kù)中讀出圖片并顯示的示例代碼
作者:Cable Fan  發(fā)表于:2005-10-11 20:33  通過(guò)Email跟作者聯(lián)系
謝謝作者,作者只是想講解一個(gè)技術(shù),并不是論方法好壞.

Re:從數(shù)據(jù)庫(kù)中讀出圖片并顯示的示例代碼
作者:青菜蟲(chóng)  發(fā)表于:2005-05-24 08:40  通過(guò)Email跟作者聯(lián)系
我想如果是存入地址的話(huà),是不是不是在本地上傳的圖片會(huì)訪(fǎng)問(wèn)不到啊。還有,我怎么沒(méi)有看明白怎么樣從網(wǎng)頁(yè)中顯示出來(lái)的啊。

Re:從數(shù)據(jù)庫(kù)中讀出圖片并顯示的示例代碼
作者:tony  發(fā)表于:2005-02-16 16:00  通過(guò)Email跟作者聯(lián)系
我和Demon 有同樣的疑問(wèn)----為什么把圖片直接放到數(shù)據(jù)庫(kù)中?為什么不存入地址,然后從數(shù)據(jù)庫(kù)讀出圖片地址就好?。?br>

Re:從數(shù)據(jù)庫(kù)中讀出圖片并顯示的示例代碼
作者:David  發(fā)表于:2004-12-16 19:59  通過(guò)Email跟作者聯(lián)系
發(fā)自?xún)?nèi)說(shuō)一句 THANK YOU VERY MUCH!!
感謝您的幫助?。?br>
Re:從數(shù)據(jù)庫(kù)中讀出圖片并顯示的示例代碼
作者:Demon  發(fā)表于:2004-08-14 16:14  通過(guò)Email跟作者聯(lián)系
為什么把圖片直接放到數(shù)據(jù)庫(kù)中?為什么不存入地址,然后從數(shù)據(jù)庫(kù)讀出圖片地址就好??!






CODE:

< !-- -- -- -- -- -- -- -- -- -- -- -- -- --servlet-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
package Photo;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;

/**

Title: 


Description: 


Copyright: Copyright (c) 2002


Company: 


* @author unascribed
* @version 1.0
*/

public class ShowImage extends HttpServlet {
    private static final String CONTENT_TYPE = "image/*";
    /**
    * 定義數(shù)據(jù)庫(kù)連接字符串,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;
    /********************************************
    * 定義應(yīng)用變量
    ******************************************/
    private String SQLString = ""; //定義查詢(xún)語(yǔ)句
    public String M_EorrMenage = ""; //定義錯(cuò)誤信息變量
    private InputStream in = null; //定義輸入流
    private int len = 10 * 1024 * 1024; //定義字符數(shù)組長(zhǎng)度

    //Initialize global variables
    public void init() throws ServletException {
        /**
        * 連接數(shù)據(jù)庫(kù)
        */
        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();
        //在數(shù)據(jù)庫(kù)中的照片的ID
        int PHOTOID = 0;
        /*********************************************
        * 接受上文傳遞的圖片ID號(hào)
        * 上文傳輸文件名稱(chēng)為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 = "請(qǐng)輸入圖片ID號(hào)";
            M_EorrMenage =
                new String(M_EorrMenage.getBytes("ISO8859_1"), "GBK");
            out.println("<%@ page contentType=‘text/html; charset=gb2312‘ %>");
            out.println("");
            out.println("");
            out.println("");
            out.println("

" + M_EorrMenage + "

");
            out.println("");

        }
        /*****************************************************
        * 執(zhí)行查詢(xún)語(yǔ)句
        *****************************************************/
        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 = "對(duì)不起,數(shù)據(jù)庫(kù)無(wú)法完成此操作!";
            M_EorrMenage =
                new String(M_EorrMenage.getBytes("ISO8859_1"), "GBK");
            response.setContentType("text/html; charset=gb2312");
            out.println("");
            out.println("");
            out.println("");
            out.println("

" + M_EorrMenage + "

");
            out.println("");

        }
        /*********************************************
        * 將圖片流讀入字符數(shù)組中,并顯示到客戶(hù)端
        ********************************************/
        try {
            if (rs.next()) {
                in = rs.getBinaryStream("photo");
                response.reset(); //返回在流中被標(biāo)記過(guò)的位置
                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(); //強(qiáng)制清出緩沖區(qū)
                toClient.close();
            } else {
                M_EorrMenage = "無(wú)此圖片!";
                M_EorrMenage =
                    new String(M_EorrMenage.getBytes("ISO8859_1"), "GBK");
                response.setContentType("text/html; charset=gb2312");
                out.println("");
                out.println(
                    "");
                out.println("");
                out.println("

" + M_EorrMenage + "

");
                out.println("");
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
            M_EorrMenage = "無(wú)法讀取圖片!";
            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("");
            out.println("");
            out.println("");
            out.println("

" + M_EorrMenage + "

");
            out.println("");
        }
    }

    //Clean up resources
    public void destroy() {
        try {
            conn.close();
        } catch (SQLException e) {
            System.err.println("aq.executeUpdate:" + e.getMessage());
            M_EorrMenage = "對(duì)不起,數(shù)據(jù)庫(kù)無(wú)法完成此操作!";
        }
    }
}










<%
int i=1;
while(i<3){
%>
 
 

<%
i++;
}
%>




注:此程序?qū)τ趶臄?shù)據(jù)庫(kù)讀取圖片后寫(xiě)入文件請(qǐng)參考代碼者留意
 


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多