成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

JSP實現(xiàn)基于WEB的數(shù)據(jù)庫圖片存儲與動態(tài)顯示

開發(fā) 后端 數(shù)據(jù)庫運維
本文將介紹如何利用JSP,實現(xiàn)基于WEB的數(shù)據(jù)庫圖片存儲和顯示的方法。在傳統(tǒng)的JSP方法中,無法存儲動態(tài)的顯示圖片,利用數(shù)據(jù)庫我們就能做到這點

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

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

建立后臺數(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

向數(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">  題目<input name="picname" type="text">  圖片<input name="pic" type="file">   <input type="Submit" name="button1" value="提交">    </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>

網(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">     <%   }   rs.close(); %></body></html>

【編輯推薦】

  1. 在 JDBC設計中加速JSP訪問數(shù)據(jù)庫
  2. 對比 JSP和ASP.NET的存儲過程
  3. 如何 從JSP頁面?zhèn)魉蛣討B(tài)圖片
責任編輯:彭凡 來源: 豆豆網(wǎng)
相關(guān)推薦

2009-06-30 09:16:45

數(shù)據(jù)庫存儲JSP文件

2009-05-08 09:17:48

動態(tài)數(shù)據(jù)庫圖片

2011-08-30 15:10:46

Qt圖片數(shù)據(jù)庫

2010-05-10 16:25:15

Oracle組件

2009-08-12 11:04:38

ASP.NET和SQL

2009-07-02 14:24:02

JSP讀取數(shù)據(jù)庫

2009-07-31 13:52:26

ASP.NET數(shù)據(jù)庫圖

2010-08-05 10:20:29

DB2數(shù)據(jù)庫動態(tài)

2011-03-23 11:11:17

圖片數(shù)據(jù)庫

2022-09-02 11:59:41

AI算法

2018-07-13 09:20:30

SQLite數(shù)據(jù)庫存儲

2009-07-03 13:56:21

JSP編程技巧

2009-07-21 13:40:24

ASP.NET上傳圖片顯示圖片

2009-06-29 18:04:32

JDOM文檔JSP

2011-09-01 14:00:11

SQL Server 存儲過程顯示表結(jié)構(gòu)

2009-06-30 15:15:30

JSP數(shù)據(jù)庫

2021-03-11 09:53:07

SpringBoot數(shù)據(jù)庫分布式鎖

2009-07-01 11:08:14

JSP DestoryJSP Init數(shù)據(jù)庫鏈接

2025-04-15 08:40:00

數(shù)據(jù)庫悲觀鎖樂觀鎖

2011-08-01 12:44:25

Oracle基于用戶管理備份與恢復
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 成人精品国产 | 九九久久精品 | 成人免费网站www网站高清 | 99小视频 | 成人a免费 | 精品久久久久久久久久久 | 美女天天操 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 欧美精品一区二区在线观看 | 日韩二三区| 男女免费在线观看视频 | 国产在线永久免费 | 综合激情av| 黄色片在线网站 | 久久久久久色 | 在线观看免费av网站 | 99久久久国产精品免费消防器 | 91久久| 国产欧美精品一区二区色综合 | 中文字幕精品一区 | 国产精品区二区三区日本 | 久久精品国产99国产 | 国内精品成人 | 亚洲综合色丁香婷婷六月图片 | 亚洲精品久久久久中文字幕欢迎你 | 精品久久一区 | 国产成人99久久亚洲综合精品 | 国产 日韩 欧美 中文 在线播放 | 一二三四在线视频观看社区 | 99re在线视频| 一级在线视频 | 中文字幕1区2区3区 亚洲国产成人精品女人久久久 | 五月婷婷激情 | www国产亚洲精品 | 91久久精品一区二区二区 | h在线看| 精品一区二区三区四区五区 | 精品国产一区二区三区久久久久久 | 日韩一区二区三区在线 | 国产成人精品午夜 | 午夜免费视频 |