新聞中心
1、引言

員工經(jīng)過(guò)長(zhǎng)期磨合與沉淀,具備了協(xié)作精神,得以通過(guò)團(tuán)隊(duì)的力量開(kāi)發(fā)出優(yōu)質(zhì)的產(chǎn)品。創(chuàng)新互聯(lián)堅(jiān)持“專注、創(chuàng)新、易用”的產(chǎn)品理念,因?yàn)椤皩W⑺詫I(yè)、創(chuàng)新互聯(lián)網(wǎng)站所以易用所以簡(jiǎn)單”。公司專注于為企業(yè)提供做網(wǎng)站、網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、電商網(wǎng)站開(kāi)發(fā),微信小程序,軟件定制設(shè)計(jì)等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。
數(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ù)訪問(wèn)技術(shù)處理圖片信息。但是,如果我們想動(dòng)態(tài)的顯示圖片,上述方法就不能滿足需要了。我們必須把圖片放入數(shù)據(jù)庫(kù)存儲(chǔ)起來(lái),然后通過(guò)編程動(dòng)態(tài)地顯示我們需要的圖片。實(shí)際操作中,可以利用JSP的編程模式來(lái)實(shí)現(xiàn)圖片的數(shù)據(jù)庫(kù)存儲(chǔ)和顯示。
2、建立后臺(tái)數(shù)據(jù)庫(kù)
假定處理的是圖片新聞,那么我們可以建立相應(yīng)的數(shù)據(jù)庫(kù)及數(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ù)庫(kù)存儲(chǔ)二進(jìn)制圖片
啟動(dòng)Dreamweaver MX后,新建一個(gè)JSP文件。其代碼如下所示。
- <%@ page contentType="text/html;charset=gb2312"%>
存儲(chǔ)圖片 TITLE> - HEAD>
- METHOD=POST ACTION="testimage.jsp">
- 新 聞 標(biāo) 題: TYPE="text" NAME="content">
- 新 聞 圖 片: TYPE="file" NAME="image">
- 新聞內(nèi)容:
- 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>
- TYPE="submit"> 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.*"%>
- <%
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- //加載驅(qū)動(dòng)程序類
- Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
- //建立數(shù)據(jù)庫(kù)聯(lián)機(jī),其中denglu為數(shù)據(jù)庫(kù)名,sa為連接數(shù)據(jù)庫(kù)的帳號(hào)及密碼。
- Statement stmt=con.createStatement();
- //建立Statement對(duì)象
- String content=request.getParameter("content");
- content=new String(content.getBytes("8859_1"),"gb2312");
- String filename=request.getParameter("image");
- filename=new String(filename.getBytes("8859_1"),"gb2312");
- String detail=request.getParameter("txtmail");
- detail=new String(detail.getBytes("8859_1"),"gb2312");
- //獲得所要顯示圖片的標(biāo)題、存儲(chǔ)路徑、內(nèi)容,并進(jìn)行中文編碼
- FileInputStream str=new FileInputStream(filename);
- String sql="insert into picturenews(content,image,detail) values(?,?,?)";
- PreparedStatement pstmt=con.prepareStatement(sql);
- pstmt.setString(1,content);
- pstmt.setBinaryStream(2,str,str.available());
- pstmt.setString(3,detail);
- pstmt.execute();
- //將數(shù)據(jù)存入數(shù)據(jù)庫(kù)
- out.println("Success,You Have Insert an Image Successfully");
- %>
4、網(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.*"%>
- <%
- 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語(yǔ)句
- 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ū)的輸入輸出到頁(yè)面
- in.read(b);
- }
- sout.flush();
- //輸入完畢,清除緩沖
- sout.close();
- }
- %>
- body>
- html>
將此文件保存為testimageout.jsp文件。下一步要做的工作就是使用HTML標(biāo)記:
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.*" %>
動(dòng)態(tài)顯示數(shù)據(jù)庫(kù)圖片 title> - head>
- <%
- 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();
- //將指針移至***一條記錄
- %>
height=99 src="testimageout.jsp?id=1" width=136> td>
- //取出***個(gè)圖片
height=99 src="testimageout.jsp?id=<%=rs.getInt("id")%>" width=136> td>
- //取出***一個(gè)圖片
- tr> table>
- body>
- html>
以上基于JSP實(shí)現(xiàn)數(shù)據(jù)庫(kù)中圖片的存儲(chǔ)與顯示的WEB應(yīng)用程序在Windows 2000 Professional/SQL Server 2000/ Apache Tomcat 4.0/JDK 1.4 JAVA環(huán)境下調(diào)試通過(guò)。
本文標(biāo)題:基于JSP實(shí)現(xiàn)數(shù)據(jù)庫(kù)中圖片的存儲(chǔ)與顯示
分享路徑:http://www.5511xx.com/article/coecchh.html


咨詢
建站咨詢
