本文将介绍如何利用JSP,实现基于WEB的数据库图片存储和显示的方法。在传统的JSP方法中,无法存储动态的显示图片,利用数据库我们就能做到这点

数据库应用程序,特别是根据WEB的数据库应用程序,常会涉及到图片信息的存储和显现 。

JSP完成根据WEB的数据库图片存储与动态显示(jsp获取数据库图片并显示)  图片存储 数据库 JSP 第1张

一般咱们运用的办法是将所要显现的图片存在特定的目录下,在数据库中保存相应的图片 的称号,在JSP中树立相应的数据源,运用数据库拜访技能处理图片信息。可是,假如咱们想 动态的显现图片,上述办法就不能满足需求了。咱们有必要把图片存入数据库,然后经过编程动 态地显现咱们需求的图片。实际操作中,能够运用JSP的编程形式来完成图片的数据库存储和 显现。

树立后台数据库

   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

向数据库存储二进制图片

发动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">  标题<input name="picname" type="text">  图片<input name="pic" type="file">   <input type="Submit" name="button1" value="提交">    </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>

网页中动态显现图片

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

   <%@ 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">     <%   }   rs.close(); %></body></html>

【修改引荐】

  1. 在 JDBC规划中责怪JSP拜访数据库
  2. 比照 JSP和ASP.NET的存储进程
  3. 怎么 从JSP页面传送动态图片
转载请说明出处
知优网 » JSP完成根据WEB的数据库图片存储与动态显示(jsp获取数据库图片并显示)

发表评论

您需要后才能发表评论