首页 > 编程 > JSP > 正文

用JSP实现数据库图片的存储与显示实例

2024-09-05 00:20:36
字体:
来源:转载
供稿:网友

  1. 引言

  数据库应用程序,特别是基于web的数据库应用程序,常会涉及到图片信息的存储和显示。

  通常我们使用的方法是将所要显示的图片存在特定的目录下,在数据库中保存相应的图片的名称,在jsp中建立相应的数据源,利用数据库访问技术处理图片信息。但是,如果我们想动态的显示图片,上述方法就不能满足需要了。我们必须把图片存入数据库,然后通过编程动态地显示我们需要的图片。实际操作中,可以利用jsp的编程模式来实现图片的数据库存储和显示。

  2. 建立后台数据库

  

if exists (select * from dbo.sysobjects
where id = object_id(n'[dbo].[p]') and objectproperty(id, n'isusertable') = 1)
drop table [dbo].[p]
go
create table [dbo].[p] (
    [picid] [int] identity (1, 1) not null ,
    [picname] [varchar] (50) collate chinese_prc_ci_as null ,
    [pic] [image] null
) on [primary] textimage_on [primary]
go

  3.向数据库存储二进制图片

  启动dreamweaver mx后,新建一个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="picname" type="text"><br>
  图片<input name="pic" type="file"><br>
  <input type="submit" name="button1" value="提交"><br>
    </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.*"%>
<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 "-//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>
<%
   request.setcharacterencoding("gb2312");
//建立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(1,picname);
pstmt.setbinarystream(2,str,str.available());
pstmt.execute();
//将数据存入数据库
out.println("success,you have insert an image successfully");
%>
</body>
</html>

  4. 网页中动态显示图片

  接下来我们要编程从数据库中取出图片,其代码如下所示。

  

<%@ page contenttype="text/html;charset=gb2312"%>
<%@ 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 "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <base href="<%=basepath%>">
  <title>my jsp 'testimageout.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>
  <%
   int id= integer.parseint(request.getparameter("picid"));
   string sql = "select pic from p where picid="+id;
   resultset rs=conn.getresult(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标记:

  

<%@ page contenttype="text/html;charset=gb2312"%>
<%@ 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 "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <base href="<%=basepath%>">
  <title>my jsp 'lookpic.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 sql = "select * from p";
   resultset rs=conn.getresult(sql);
    while(rs.next())
    {
 %>
  <ccid_file values="testimageout" % />" width="100" height="100">
   <br>
 <%
   }
   rs.close();
 %>
</body>
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表