用JSP實現(xiàn)數(shù)據(jù)庫圖片的存儲與顯示實例
來源:易賢網(wǎng) 閱讀:733 次 日期:2015-01-27 14:49:51
溫馨提示:易賢網(wǎng)小編為您整理了“用JSP實現(xiàn)數(shù)據(jù)庫圖片的存儲與顯示實例”,方便廣大網(wǎng)友查閱!

1. 引言

數(shù)據(jù)庫應(yīng)用程序,特別是基于WEB的數(shù)據(jù)庫應(yīng)用程序,常會涉及到圖片信息的存儲和顯示。

通常我們使用的方法是將所要顯示的圖片存在特定的目錄下,在數(shù)據(jù)庫中保存相應(yīng)的圖片的名稱,在JSP中建立相應(yīng)的數(shù)據(jù)源,利用數(shù)據(jù)庫訪問技術(shù)處理圖片信息。但是,如果我們想動態(tài)的顯示圖片,上述方法就不能滿足需要了。我們必須把圖片存入數(shù)據(jù)庫,然后通過編程動態(tài)地顯示我們需要的圖片。實際操作中,可以利用JSP的編程模式來實現(xiàn)圖片的數(shù)據(jù)庫存儲和顯示。

2. 建立后臺數(shù)據(jù)庫

if exists (select * from dbo.sysobjects

where id = object_id(N'[dbo].[p]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[p]

GO

CREATE TABLE [dbo].[p] (

[picid] [int] IDENTITY (1, 1) NOT NULL ,

[picname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,

[pic] [image] NULL

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

3.向數(shù)據(jù)庫存儲二進制圖片

啟動Dreamweaver MX后,新建一個JSP文件。其代碼如下所示。

<%@ page contentType="text/html;charset=gb2312"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()

+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'InputImage.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<form action="testimage.jsp" method="POST"><br>

題目<input name="picname" type="text"><br>

圖片<input name="pic" type="file"><br>

<input type="Submit" name="button1" value="提交"><br>

</form>

</body>

</html>

將此文件保存為InputImage.jsp文件,其中testimage.jsp文件是用來將圖片數(shù)據(jù)存入數(shù)據(jù)庫的,具體代碼如下所示:

<%@ page contentType="text/html;charset=gb2312"%>

<%@ page import="java.sql.*" %>

<%@ page import="java.util.*"%>

<%@ page import="java.text.*"%>

<%@ page import="java.io.*"%>

<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+

":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'testimage.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<%

request.setCharacterEncoding("gb2312");

//建立Statement對象

String picname=request.getParameter("picname");

String pic=request.getParameter("pic");

//獲得所要顯示圖片的標(biāo)題、存儲路徑、內(nèi)容,并進行中文編碼

FileInputStream str=new FileInputStream(pic);

String sql="insert into p(picname,pic) values(?,?)";

PreparedStatement pstmt=conn.getPreparedStatement(sql);

pstmt.setString(1,picname);

pstmt.setBinaryStream(2,str,str.available());

pstmt.execute();

//將數(shù)據(jù)存入數(shù)據(jù)庫

out.println("Success,You Have Insert an Image Successfully");

%>

</body>

</html>

4. 網(wǎ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.*"%>

<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+

":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'testimageout.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<%

int id= Integer.parseInt(request.getParameter("picid"));

String sql = "select pic from p WHERE picid="+id;

ResultSet rs=conn.getResult(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)記:

<%@ page contentType="text/html;charset=gb2312"%>

<%@ page import="java.sql.*" %>

<%@ page import="java.util.*"%>

<%@ page import="java.text.*"%>

<%@ page import="java.io.*"%>

<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+

":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'lookpic.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<%

String sql = "select * from p";

ResultSet rs=conn.getResult(sql);

while(rs.next())

{

%>

<ccid_file values="testimageout" % />" width="100" height="100">

<br>

<%

}

rs.close();

%>

</body>

</html>

更多信息請查看IT技術(shù)專欄

更多信息請查看腳本欄目
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)