基于JSP实现数据库中图片的存储与显示(已经通过测试)

1说明

我用的是ORACLE数据库,在网上搜了一些相关的存储显示代码,不过,基本都是一个版本且带有错误,经过我的修改后,以下代码已通过测试

2、建立后台数据库

create table image (name varchar2(20) primary key,

int number(6),image(blob))

注释:照片在数据库中以二进制存储,故image字段为blob型 

3创建几个jsp文件

3.1以下文件为InputImage.jsp.

<%@ page contentType="text/html;charset=gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()
+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <base href="<%=basePath%>">
   <title>My JSP 'InputImage.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
   <form action="testimage.jsp" method="POST"><br>
   题目<input name="name" type="text"><br>
   图片<input name="image" type="file"><br>
   <input type="Submit" name="button1" value="提交"><br>
</form>
</body>
</html>

3.2以下文件为testimage.jsp.

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 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="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%

String sConn = "jdbc:oracle:thin:@localhost:1521:testorcl";
Connection conn = null;//创建Connection引用

    String userName = "webdb";
 /**设置数据库连接密码*/
 String pwd = "webdb";
 /**设置数据库*/
 String dbDriver = "oracle.jdbc.driver.OracleDriver";
 /**设置数据库URL*/
 
 Class.forName(dbDriver).newInstance();
 conn = DriverManager.getConnection(sConn, userName, pwd);
    Statement statement = conn.createStatement();
    PreparedStatement pstmt = null ;

   request.setCharacterEncoding("gb2312");
//建立Statement对象
String name=request.getParameter("name");
String image=request.getParameter("image");
FileInputStream str=new FileInputStream(image);
String sql="insert into image(name,image) values(?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
//将数据存入数据库
pstmt.close();
conn.close();
%>
</body>
</html>

3.3以下文件为testimageout.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>
<%
String sConn = "jdbc:oracle:thin:@localhost:1521:testorcl";

Connection conn = null;//创建Connection引用

    String userName = "webdb";
 /**设置数据库连接密码*/
 String pwd = "webdb";
 /**设置数据库*/
 String dbDriver = "oracle.jdbc.driver.OracleDriver";
 /**设置数据库URL*/
 
 Class.forName(dbDriver).newInstance();
 conn = DriverManager.getConnection(sConn, userName, pwd);
    Statement statement = conn.createStatement();
    ResultSet rs=null;
int id= Integer.parseInt(request.getParameter("id"));
//获得所要显示图片的编号id,并转换为整型
String sql = "select image from image WHERE id="+id+"";
//要执行查询的SQL语句
rs=statement.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();
}
conn.close();
%>
</body>
</html>
3.4一下文件为lookpic.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>
搞定,这样,打开InputImage.jsp即可导入图片,打开lookpic.jsp文件即可显示图片!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值