国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

用JSP實現(xiàn)數(shù)據(jù)庫圖片的存儲與顯示實例

2024-09-05 00:20:36
字體:
來源:轉載
供稿:網(wǎng)友

  1. 引言

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

  通常我們使用的方法是將所要顯示的圖片存在特定的目錄下,在數(shù)據(jù)庫中保存相應的圖片的名稱,在jsp中建立相應的數(shù)據(jù)源,利用數(shù)據(jù)庫訪問技術處理圖片信息。但是,如果我們想動態(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");
//獲得所要顯示圖片的標題、存儲路徑、內(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標記:

  

<%@ 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>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 道真| 凤山市| 英超| 余干县| 山东| 乌兰察布市| 荔波县| 兴宁市| 临安市| 洮南市| 清水河县| 柳州市| 湖州市| 汉寿县| 昆明市| 新津县| 浦东新区| 类乌齐县| 金沙县| 清原| 开封县| 洛宁县| 江永县| 怀集县| 肇东市| 同心县| 杭锦旗| 平泉县| 班戈县| 商都县| 南华县| 石首市| 武汉市| 乐亭县| 米林县| 焦作市| 寿宁县| 麦盖提县| 阆中市| 沙湾县| 桂阳县|