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

开发 后端
本文向您介绍使用JSP时限数据库图片的存储与显示,利用JSP的编程模式来实现图片的动态显示。

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﹥

 

【编辑推荐】

  1. JDBC与JSP简单模拟MVC三层架构
  2. JSP技术优缺点深入分析
  3. JSP中Action属性的功能浅析
责任编辑:佚名 来源: ChinaItLab
相关推荐

2009-06-30 09:16:45

数据库存储JSP文件

2009-03-23 09:01:00

图片存储数据库JSP

2011-08-30 15:10:46

Qt图片数据库

2011-07-13 16:19:54

存储过程SQL Server数

2009-07-02 14:24:02

JSP读取数据库

2009-08-12 11:04:38

ASP.NET和SQL

2009-07-31 13:52:26

ASP.NET数据库图

2011-03-23 11:11:17

图片数据库

2019-07-05 11:20:31

PythonMySQL数据库

2009-07-21 13:40:24

ASP.NET上传图片显示图片

2018-07-13 09:20:30

SQLite数据库存储

2011-09-01 14:00:11

SQL Server 存储过程显示表结构

2009-07-03 13:56:21

JSP编程技巧

2009-06-29 18:04:32

JDOM文档JSP

2009-06-30 15:15:30

JSP数据库

2009-07-01 11:08:14

JSP DestoryJSP Init数据库链接

2010-05-31 09:44:38

2010-05-20 11:23:12

卸载MySQL

2009-03-17 09:28:22

接口VCMySQL

2018-12-26 15:00:56

数据库行式存储列式存储
点赞
收藏

51CTO技术栈公众号