摘 要:本文介绍了利用JSP的编程模式如何实现图片的数据库存储和显示。
关键词:JSP、动态存取、SQL、输入输出流
收集整理:晨风教程网(http://www.Net118.com)
1、引言
数据库应用程序,特别是基于WEB的数据库应用程序,常会涉及到图片信息的存储和显示。通常我们使用的方法是将所要显示的图片存在特定的目录下,在数据库中保存相应的图片的名称,在JSP中建立相应的数据源,利用数据库访问技术处理图片信息。但是,如果我们想动态的显示图片,上述方法就不能满足需要了。我们必须把图片存入数据库,然后通过编程动态地显示我们需要的图片。实际操作中,可以利用JSP的编程模式来实现图片的数据库存储和显示。
2、 建立后台数据库
假定处理的是图片新闻,那么我们可以建立相应的数据库及数据表对象。我们要存取的数据表结构的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作为标识,每存储一行数据,自动增加1。字段image
用于存储图片信息,其数据类型为“image”。
3、向数据库存储二进制图片
启动Dreamweaver MX后,新建一个JSP文件。其代码如下所示。
<%
...
@ page contentType="text/html;charset=gb2312"
%>
<
HTML
>
<
HEAD
>
<
TITLE
>
存储图片
</
TITLE
>
</
HEAD
>
<
body
>
<!--
下面的窗体将以Post方法,将数据传递给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文件是用来将图片数据存入数据库的,具体代码如下所示:
<%
@ 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(
"
sun.jdbc.odbc.JdbcOdbcDriver
"
);
//
加载驱动程序类
Connection con
=
DriverManager.getConnection(
"
jdbc:odbc:denglu
"
,
"
sa
"
,
"
sa
"
);
//
建立数据库联机,其中denglu为数据库名,sa为连接数据库的帐号及密码。
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();
//
将数据存入数据库
out.println(
"
Success,You Have Insert an Image Successfully
"
);
%>
4、网页中动态显示图片
接下来我们要编程从数据库中取出图片,其代码如下所示。
<%
@ 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(
"
sun.jdbc.odbc.JdbcOdbcDriver
"
);
//
加载驱动程序类
Connection con
=
DriverManager.getConnection(
"
jdbc:odbc:denglu
"
,
"
sa
"
,
"
sa
"
);
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
+
""
;
//
要执行查询的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);
//将缓冲区的输入输出到页面
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
>
动态显示数据库图片
</
title
>
</
head
>
<
body
>
<%
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();
//
将指针移至最后一条记录
%>
<
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 2000 Professional/SQL Server 2000/ Apache Tomcat 4.0/JDK 1.4 JAVA环境下调试通过。