將此文件保存為InputImagejsp文件其中testimagejsp文件是用來將圖片數據存入數據庫的具體代碼如下所示
<%@ page contentType=
text/html;charset=gb
%>
<%@ page import=
java
sql
*
%>
<%@ page import=
java
util
*
%>
<%@ page import=
java
text
*
%>
<%@ page import=
java
io
*
%>
<jsp:useBean id=
conn
scope=
page
class=
dbconn
DBResult
/>
<%
String path = request
getContextPath();
String basePath = request
getScheme()+
://
+request
getServerName()+
:
+request
getServerPort()+path+
/
;
%>
<!DOCTYPE HTML PUBLIC
//W
C//DTD HTML
Transitional//EN
>
<html>
<head>
<base href=
<%=basePath%>
>
<title>My JSP
testimage
jsp
starting page</title>
<meta http
equiv=
pragma
content=
no
cache
>
<meta http
equiv=
cache
control
content=
no
cache
>
<meta http
equiv=
expires
content=
>
<meta http
equiv=
keywords
content=
keyword
keyword
keyword
>
<meta http
equiv=
description
content=
This is my page
>
<!
<link rel=
stylesheet
type=
text/css
href=
styles
css
>
>
</head>
<body>
<%
request
setCharacterEncoding(
gb
);
//建立Statement對象
String picname=request
getParameter(
picname
);
String pic=request
getParameter(
pic
);
//獲得所要顯示圖片的標題
存儲路徑
內容
並進行中文編碼
FileInputStream str=new FileInputStream(pic);
String sql=
insert into p(picname
pic) values(?
?)
;
PreparedStatement pstmt=conn
getPreparedStatement(sql);
pstmt
setString(
picname);
pstmt
setBinaryStream(
str
str
available());
pstmt
execute();
//將數據存入數據庫
out
println(
Success
You Have Insert an Image Successfully
);
%>
</body>
</html>
網頁中動態顯示圖片
接下來我們要編程從數據庫中取出圖片其代碼如下所示
[] [] [] []
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20706.html