基于JSP實(shí)現(xiàn)數(shù)據(jù)庫中圖片的存儲(chǔ)與顯示
1、引言
數(shù)據(jù)庫應(yīng)用程序,特別是基于WEB的數(shù)據(jù)庫應(yīng)用程序,常會(huì)涉及到圖片信息的存儲(chǔ)和顯示。通常我們使用的方法是將所要顯示的圖片存在特定的目錄下,在數(shù)據(jù)庫中保存相應(yīng)的圖片的名稱,在JSP中建立相應(yīng)的數(shù)據(jù)源,利用數(shù)據(jù)庫訪問技術(shù)處理圖片信息。但是,如果我們想動(dòng)態(tài)的顯示圖片,上述方法就不能滿足需要了。我們必須把圖片放入數(shù)據(jù)庫存儲(chǔ)起來,然后通過編程動(dòng)態(tài)地顯示我們需要的圖片。實(shí)際操作中,可以利用JSP的編程模式來實(shí)現(xiàn)圖片的數(shù)據(jù)庫存儲(chǔ)和顯示。
2、建立后臺(tái)數(shù)據(jù)庫
假定處理的是圖片新聞,那么我們可以建立相應(yīng)的數(shù)據(jù)庫及數(shù)據(jù)表對(duì)象。我們要存取的數(shù)據(jù)表結(jié)構(gòu)的SQL腳本如下所示:
- if exists (select * from dbo.sysobjects where id =
- object_id(N'[dbo].[picturenews]') andOBJECTPROPERTY(id, N'IsUserTable') = 1)
- drop table [dbo].[picturenews]
- GO
- CREATE TABLE [dbo].[picturenews] (
- [id] [int] IDENTITY (1, 1) NOT NULL ,
- [image] [image] NULL ,
- [content] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
- [detail] [varchar] (5000) COLLATE Chinese_PRC_CI_AS NULL
- ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
- GO
表picturenews中,字段id作為標(biāo)識(shí),每存儲(chǔ)一行數(shù)據(jù),自動(dòng)增加1。字段image
用于存儲(chǔ)圖片信息,其數(shù)據(jù)類型為“image”。
3、向數(shù)據(jù)庫存儲(chǔ)二進(jìn)制圖片
啟動(dòng)Dreamweaver MX后,新建一個(gè)JSP文件。其代碼如下所示。
- <%@ page contentType="text/html;charset=gb2312"%>
- <HTML>
- <HEAD>
- <TITLE>存儲(chǔ)圖片TITLE>
- HEAD>
- <body>
- <FORM METHOD=POST ACTION="testimage.jsp">
- 新 聞 標(biāo) 題:<INPUT TYPE="text" NAME="content"><BR>
- 新 聞 圖 片:<INPUT TYPE="file" NAME="image"><BR>
- 新聞內(nèi)容:
- <TEXTAREA name="txtmail" rows="15" cols="90"
- style="BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid;
- BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; FONT-SIZE: 9pt;
- HEIGHT: 200px; WIDTH: 100%" wrap="physical" >TEXTAREA><br>
- <INPUT TYPE="submit">form>
- body>
- HTML>
將此文件保存為InputImage.jsp文件,其中testimage.jsp文件是用來將圖片數(shù)據(jù)存入數(shù)據(jù)庫的,具體代碼如下所示:
- <%@ page contentType="text/html;charset=gb2312"%>
- <%@ page import="java.sql.*" %>
4、網(wǎng)頁中動(dòng)態(tài)顯示圖片
接下來我們要編程從數(shù)據(jù)庫中取出圖片,其代碼如下所示。
- <%@ page contentType="text/html;charset=gb2312"%>
- <%@ page import="java.sql.*" %>
- <%@ page import="java.util.*"%>
- <%@ page import="java.text.*"%>
- <%@ page import="java.io.*"%>
- <html>
- <body>
- <%
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- //加載驅(qū)動(dòng)程序類
- Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
- Statement stmt=con.createStatement();
- ResultSet rs=null;
- //建立ResultSet(結(jié)果集)對(duì)象
- int id= Integer.parseInt(request.getParameter("id"));
- //獲得所要顯示圖片的編號(hào)id,并轉(zhuǎn)換為整型
- String sql = "select image from picturenews WHERE id="+id+"";
- //要執(zhí)行查詢的SQL語句
- rs=stmt.executeQuery(sql);
- while(rs.next()) {
- ServletOutputStream sout = response.getOutputStream();
- //圖片輸出的輸出流
- InputStream in = rs.getBinaryStream(1);
- byte b[] = new byte[0x7a120];
- for(int i = in.read(b); i != -1;)
- {
- sout.write(b);
- //將緩沖區(qū)的輸入輸出到頁面
- in.read(b);
- }
- sout.flush();
- //輸入完畢,清除緩沖
- sout.close();
- }
- %>
- body>
- html>
將此文件保存為testimageout.jsp文件。下一步要做的工作就是使用HTML標(biāo)記:
- <IMG src="testimageout.jsp?id=<%=rs.getInt("id")%>" width=100 height=100>
取出所要顯示的圖片,其中id是所要取出圖片的編號(hào)。本例中我們輸出了***個(gè)和***一個(gè)圖片信息,詳細(xì)的程序代碼如下所示。
- <%@ page contentType="text/html;charset=gb2312"%>
- <%@ page import="java.sql.*" %>
- <html>
- <head>
- <title>動(dòng)態(tài)顯示數(shù)據(jù)庫圖片title>
- head>
- <body>
- <%
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
- Statement stmt=con.createStatement();
- String sql=new String();
- sql= "select * from picturenews";
- ResultSet rs=stmt.executeQuery(sql);
- rs.last();
- //將指針移至***一條記錄
- %>
- <table>
- <tr><td><IMG height=99 src="testimageout.jsp?id=1" width=136>td>
- //取出***個(gè)圖片
- <td><IMG height=99 src="testimageout.jsp?id=<%=rs.getInt("id")%>" width=136>td>
- //取出***一個(gè)圖片
- tr>table>
- body>
- html>
以上基于JSP實(shí)現(xiàn)數(shù)據(jù)庫中圖片的存儲(chǔ)與顯示的WEB應(yīng)用程序在Windows 2000 Professional/SQL Server 2000/ Apache Tomcat 4.0/JDK 1.4 JAVA環(huán)境下調(diào)試通過。
【編輯推薦】