亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2015年10月13日 12:29:45   作者:baggio7095586  
這篇文章主要介紹了JSP中圖片的上傳與顯示方法,以實例形式較為詳細的分析了數(shù)據(jù)庫的創(chuàng)建、圖片存儲、圖片文件的傳輸及頁面顯示等相關實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了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>
新聞內容:<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");
//獲得所要顯示圖片的標題、存儲路徑、內容,并進行中文編碼
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)境下調試通過。

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

相關文章

最新評論