基於JSP實現資料庫中圖片的儲存與顯示

來源:互聯網
上載者:User

資料庫應用程式,特別是基於WEB的資料庫應用程式,常會涉及到圖片資訊的儲存和顯示。通常我們使用的方法是將所要顯示的圖片存在特定的目錄下,在資料庫中儲存相應的圖片的名稱,在JSP中建立相應的資料來源,利用資料庫訪問技術處理圖片資訊。但是,如果我們想動態顯示圖片,上述方法就不能滿足需要了。我們必須把圖片存入資料庫,然後通過編程動態地顯示我們需要的圖片。實際操作中,可以利用JSP的編程模式來實現圖片的資料庫儲存和顯示。

1.建立MSsqlserver資料庫test,表pictruenews;
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
2.加入mssqlserver的jar包
3.四個jsp
add.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>儲存圖片</TITLE>
</HEAD>
<body>
<!-- 下面的表單將以Post方法,將資料傳遞給testimage.jsp檔案 -->
<FORM METHOD=POST ACTION="InputImage.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  存圖片
<%@ 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=test","sa","sa");
Statement stmt=con.createStatement();
String content=request.getParameter("content");
content=new String(content.getBytes("8859_1"),"gb2312");

String detail=request.getParameter("txtmail");
detail=new String(detail.getBytes("8859_1"),"gb2312");

String sql="insert into picturenews(content,image,detail) values(?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,content);

String filename=new String(request.getParameter("image").getBytes("8859_1"),"gb2312");

//D:/photo/美女/221.jpg
FileInputStream str=new FileInputStream(filename);

//FileInputStream(String name)
// 通過開啟一個到實際檔案的串連來建立一個 fileinputstream,該檔案通過檔案系統中的路徑名 name 指定。
pstmt.setBinaryStream(2,str,str.available());

//InputStream.available() 返回下一次對此輸入資料流調用的方法可以不受阻塞地從此輸入資料流讀取(或跳過)的估計剩餘位元組數。
//preparedStatement.setBinaryStream(int parameterIndex, InputStream x, int length)
pstmt.setString(3,detail);
pstmt.execute();
//將資料存入資料庫
out.println("Success,You Have Insert an Image Successfully");
%>

testimageout.jsp 根據id取圖片
<%@ 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=test","sa","sa");
Statement stmt=con.createStatement();
ResultSet rs=null;
int id= Integer.parseInt(request.getParameter("id"));
String sql = "select image from picturenews WHERE id="+id+"";
rs=stmt.executeQuery(sql);
while(rs.next()) {
ServletOutputStream sout = response.getOutputStream();
InputStream in = rs.getBinaryStream("image");
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)//?????????????? 少一項

//read()從輸入資料流中讀取一定數量的位元組,並將其儲存在緩衝區數組 b 中。如果因為流位於檔案末尾而沒有可用的位元組,則傳回值 -1; 方法的效果等同於:read(b, 0, b.length)
{
sout.write(b);

//將緩衝區的輸入輸出到頁面
in.read(b);
}
sout.flush();
//輸入完畢,清除緩衝
sout.close();
}
%>
</body>
</html>

index.jsp 顯示圖片的一個例子
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>動態顯示資料庫圖片</title>
</head>
<body>
<table>
<tr><td><IMG height=99 src="testimageout.jsp?id=1" width=136></td>
//取出第一個圖片
</tr></table>
</body>
</html>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.