主頁 > 知識庫 > JSP中圖片的上傳與顯示方法實例詳解

JSP中圖片的上傳與顯示方法實例詳解

熱門標簽:Linux服務器 Mysql連接數(shù)設置 服務器配置 科大訊飛語音識別系統(tǒng) 團購網(wǎng)站 阿里云 銀行業(yè)務 電子圍欄

本文實例講述了JSP中圖片的上傳與顯示方法。分享給大家供大家參考。具體如下:

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ù)庫

假定處理的是圖片新聞,那么我們可以建立相應的數(shù)據(jù)庫及數(shù)據(jù)表對象。我們要存取的數(shù)據(jù)表結構的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作為標識,每存儲一行數(shù)據(jù),自動增加1。字段image
用于存儲圖片信息,其數(shù)據(jù)類型為“image”。
 
3、向數(shù)據(jù)庫存儲二進制圖片

新建一個JSP文件。其代碼如下所示。

%@ page contentType="text/html;charset=gb2312"%>
HTML>
HEAD>
TITLE>存儲圖片/TITLE>
/HEAD>
body>
!-- 下面的窗體將以Post方法,將數(shù)據(jù)傳遞給testimage.jsp文件 -->
FORM METHOD=POST ACTION="testimage.jsp">
新 聞 標 題:INPUT TYPE="text" NAME="content">BR>
新 聞 圖 片:INPUT TYPE="file" NAME="image">BR>
新聞內(nèi)容:TEXTAREA 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>br>
INPUT TYPE="submit">/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.*"%>
html>
body>
%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加載驅動程序類
 Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa");
//建立數(shù)據(jù)庫聯(lián)機,其中upload_Image為數(shù)據(jù)庫名,sa為連接數(shù)據(jù)庫的帳號及密碼。
Statement stmt=con.createStatement();
//建立Statement對象
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");
//獲得所要顯示圖片的標題、存儲路徑、內(nèi)容,并進行中文編碼
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ù)庫
out.println("Success,You Have Insert an Image Successfully");
%>

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.*"%>
html>
body>
%
%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加載驅動程序類
 Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa");
//建立數(shù)據(jù)庫聯(lián)機,其中upload_Image為數(shù)據(jù)庫名,sa為連接數(shù)據(jù)庫的帳號及密碼。
Statement stmt=con.createStatement();
ResultSet rs=null;
//建立ResultSet(結果集)對象
int id= Integer.parseInt(request.getParameter("id"));
//獲得所要顯示圖片的編號id,并轉換為整型
String sql = "select image from picturenews WHERE id="+id+"";
//要執(zhí)行查詢的SQL語句
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ū)的輸入輸出到頁面
in.read(b);
}
sout.flush();
//輸入完畢,清除緩沖
sout.close();
}
%>
/body>
/html>

將此文件保存為testimageout.jsp文件。下一步要做的工作就是使用HTML標記:

IMG src="testimageout.jsp?id=%=rs.getInt("id")%>"  width=100 height=100>
取出所要顯示的圖片,其中id是所要取出圖片的編號。本例中我們輸出了第一個和最后一個圖片信息,詳細的程序代碼如下所示。

%@ page contentType="text/html;charset=gb2312"%>
%@ page import="java.sql.*" %>
html>
head>
title>動態(tài)顯示數(shù)據(jù)庫圖片/title>
/head>
body>
%
%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加載驅動程序類
 Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa");
//建立數(shù)據(jù)庫聯(lián)機,其中upload_Image為數(shù)據(jù)庫名,sa為連接數(shù)據(jù)庫的帳號及密碼。
Statement stmt=con.createStatement();
String sql=new String();
sql= "select * from picturenews";
ResultSet rs=stmt.executeQuery(sql);
rs.last();
//將指針移至最后一條記錄
%> 
table>
tr>td>IMG height=99 src="testimageout.jsp?id=1" width=136>/td>
//取出第一個圖片
td>IMG height=99 src="testimageout.jsp?id=%=rs.getInt("id")%>" width=136>/td>
//取出最后一個圖片
/tr>/table>
/body>
/html>

以上WEB應用程序在Windows xp/SQL Server 2000/ Apache Tomcat 4.0/Jbuilder環(huán)境下調(diào)試通過。

希望本文所述對大家的JSP程序設計有所幫助。

您可能感興趣的文章:
  • jsp圖片效果大全(圖像震動效果、閃爍效果、自動切換圖像)
  • Jsp中如何讓圖片在div中居中
  • 解決圖片路徑中含有中文在jsp下不能正常顯示問題
  • 如何在jsp界面中插入圖片

標簽:蚌埠 棗莊 江蘇 衢州 大理 衡水 萍鄉(xiāng) 廣元

巨人網(wǎng)絡通訊聲明:本文標題《JSP中圖片的上傳與顯示方法實例詳解》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266