JSP如何實(shí)現(xiàn)基于WEB的數(shù)據(jù)庫(kù)圖片存儲(chǔ)與動(dòng)態(tài)顯示

這篇文章給大家分享的是有關(guān)JSP如何實(shí)現(xiàn)基于WEB的數(shù)據(jù)庫(kù)圖片存儲(chǔ)與動(dòng)態(tài)顯示的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來(lái)看看吧。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、虛擬空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、鐘祥網(wǎng)站維護(hù)、網(wǎng)站推廣。

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

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

建立后臺(tái)數(shù)據(jù)庫(kù)

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

向數(shù)據(jù)庫(kù)存儲(chǔ)二進(jìn)制圖片

啟動(dòng)Dreamweaver MX后,新建一個(gè)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">題目<input 

name="picname" type="text">圖片<input name="pic" type="file">

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

</body></html>

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

<%@ 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對(duì)象String picname=request.getParameter("picname");String 

pic=request.getParameter("pic");//獲得所要顯示圖片的標(biāo)題、存儲(chǔ)路徑、內(nèi)容,并進(jìn)行中

文編碼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ù)庫(kù)out.println("Success,You Have Insert an 

Image Successfully");%></body></html>

網(wǎng)頁(yè)中動(dòng)態(tài)顯示圖片

接下來(lái)我們要編程從數(shù)據(jù)庫(kù)中取出圖片,其代碼如下所示。

<%@ 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ū)的輸入

輸出到頁(yè)面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">

<% } rs.close(); %></body></html>

感謝各位的閱讀!關(guān)于“JSP如何實(shí)現(xiàn)基于WEB的數(shù)據(jù)庫(kù)圖片存儲(chǔ)與動(dòng)態(tài)顯示”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

文章題目:JSP如何實(shí)現(xiàn)基于WEB的數(shù)據(jù)庫(kù)圖片存儲(chǔ)與動(dòng)態(tài)顯示
文章源于:http://bm7419.com/article12/pschgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站響應(yīng)式網(wǎng)站、云服務(wù)器、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)