网站建设资讯

NEWS

网站建设资讯

JSP如何实现数据库中图片的存储与显示

本篇内容主要讲解“JSP如何实现数据库中图片的存储与显示”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“JSP如何实现数据库中图片的存储与显示”吧!

创新互联客户idc服务中心,提供西信服务器托管、成都服务器、成都主机托管、成都双线服务器等业务的一站式服务。通过各地的服务中心,我们向成都用户提供优质廉价的产品以及开放、透明、稳定、高性价比的服务,资深网络工程师在机房提供7*24小时标准级技术保障。

1、引言

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

2、建立后台数据库

假定处理的是图片新闻,那么我们可以建立相应的数据库及数据表对象。我们要存取的数据表结构的SQL脚本如下所示:

if exists (select * from dbo.sysobjects where id =   object_id(N'[dbo].[picturenews]') andOBJECTPROPERTY(id, N'IsUserTable') = 1)  drop table [dbo].[picturenews]  GO  CREATE TABLE [dbo].[picturenews] (  [id] [int] IDENTITY (1, 1) NOT NULL ,  [image] [image] NULL ,  [content] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,  [detail] [varchar] (5000) COLLATE Chinese_PRC_CI_AS NULL   ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  GO

表picturenews中,字段id作为标识,每存储一行数据,自动增加1。字段image

用于存储图片信息,其数据类型为“image”。

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

启动Dreamweaver MX后,新建一个JSP文件。其代码如下所示。

<%@ page contentType="text/html;charset=gb2312"%>     存储图片TITLE> HEAD> <body>  <FORM METHOD=POST ACTION="testimage.jsp"> 新 闻 标 题:<INPUT TYPE="text" NAME="content"><BR> 新 闻 图 片:<INPUT TYPE="file" NAME="image"><BR> 新闻内容:  <TEXTAREA name="txtmail" rows="15" cols="90"   style="BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid;   BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; FONT-SIZE: 9pt;   HEIGHT: 200px; WIDTH: 100%" wrap="physical" >TEXTAREA><br> <INPUT TYPE="submit">form> body> HTML></pre><p>将此文件保存为InputImage.jsp文件,其中testimage.jsp文件是用来将图片数据存入数据库的,具体代码如下所示:</p><pre><%@ page contentType="text/html;charset=gb2312"%>   <%@ page import="java.sql.*" %></pre><ul><li><p><strong><</strong>%@ page import="java.util.*"%<strong>></strong> </p></li><li><p><strong><</strong>%@ page import="java.text.*"%<strong>></strong> </p></li><li><p><strong><</strong>%@ page import="java.io.*"%<strong>></strong> </p></li><li><p><strong><html></strong>   </p></li><li><p><strong><body></strong>   </p></li><li><p><strong><</strong>%  </p></li><li><p>Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  </p></li><li><p>//加载驱动程序类  </p></li><li><p>Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");  </p></li><li><p>//建立数据库联机,其中denglu为数据库名,sa为连接数据库的帐号及密码。  </p></li><li><p>Statement stmt=con.createStatement();   </p></li><li><p>//建立Statement对象  </p></li><li><p>String content=request.getParameter("content");  </p></li><li><p>content=new String(content.getBytes("8859_1"),"gb2312");  </p></li><li><p>String filename=request.getParameter("image");  </p></li><li><p>filename=new String(filename.getBytes("8859_1"),"gb2312");  </p></li><li><p>String detail=request.getParameter("txtmail");  </p></li><li><p>detail=new String(detail.getBytes("8859_1"),"gb2312");  </p></li><li><p>//获得所要显示图片的标题、存储路径、内容,并进行中文编码  </p></li><li><p>FileInputStream str=new FileInputStream(filename);  </p></li><li><p>String sql="insert into picturenews(content,image,detail) values(?,?,?)";  </p></li><li><p>PreparedStatement pstmt=con.prepareStatement(sql);  </p></li><li><p>pstmt.setString(1,content);  </p></li><li><p>pstmt.setBinaryStream(2,str,str.available());  </p></li><li><p>pstmt.setString(3,detail);  </p></li><li><p>pstmt.execute();  </p></li><li><p>//将数据存入数据库  </p></li><li><p>out.println("Success,You Have Insert an Image Successfully");  </p></li><li><p>%<strong>></strong> </p></li><p><strong>4、网页中动态显示图片</strong></p><p>接下来我们要编程从数据库中取出图片,其代码如下所示。</p><pre><%@ page contentType="text/html;charset=gb2312"%>   <%@ page import="java.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%>   <html> <body> <%  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   //加载驱动程序类  Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");  Statement stmt=con.createStatement();  ResultSet rs=null;   //建立ResultSet(结果集)对象  int id= Integer.parseInt(request.getParameter("id"));  //获得所要显示图片的编号id,并转换为整型  String sql = "select image from picturenews WHERE id="+id+"";   //要执行查询的SQL语句  rs=stmt.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();  }  %> body> html></pre><p>将此文件保存为testimageout.jsp文件。下一步要做的工作就是使用HTML标记:</p><pre><IMG src="testimageout.jsp?id=<%=rs.getInt("id")%>" width=100 height=100></pre><p>取出所要显示的图片,其中id是所要取出图片的编号。本例中我们输出了***个和***一个图片信息,详细的程序代码如下所示。</p><pre><%@ page contentType="text/html;charset=gb2312"%>   <%@ page import="java.sql.*" %> <html> <head> <title>动态显示数据库图片title> head> <body> <%   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");  Statement stmt=con.createStatement();  String sql=new String();  sql= "select * from picturenews";  ResultSet rs=stmt.executeQuery(sql);  rs.last();  //将指针移至***一条记录  %>   <table> <tr><td><IMG height=99 src="testimageout.jsp?id=1" width=136>td> //取出***个图片  <td><IMG height=99 src="testimageout.jsp?id=<%=rs.getInt("id")%>" width=136>td> //取出***一个图片  tr>table> body> html></pre></ul><p>到此,相信大家对“JSP如何实现数据库中图片的存储与显示”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!</p>            
            
                        <br>
            分享标题:JSP如何实现数据库中图片的存储与显示            <br>
            路径分享:<a href="http://cdweb.net/article/ipojjo.html">http://cdweb.net/article/ipojjo.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/ephhh.html">如何在IDEA中导入Eclipse项目-创新互联</a>
                </li><li>
                    <a href="/article/epheh.html">如何管理归档文件和归档目录-创新互联</a>
                </li><li>
                    <a href="/article/ephip.html">python3正则表达式基础廖雪峰-创新互联</a>
                </li><li>
                    <a href="/article/ephjg.html">lua读取lua文件-创新互联</a>
                </li><li>
                    <a href="/article/ehood.html">搭建pythondjango虚拟环境的操作步骤-创新互联</a>
                </li>        </ul>
    </div>
</div>
</div>
<footer>
    <div class="footop">
        <div class="wrap">
            <div class="bottomrpw">
                <div class="erp arp">
                    <dl>
                        <dt>ADDRESS</dt>
                        <dd class="address"> <i class="icon"></i> <span class="word">成都市青羊区锦天国际1号楼1002室</span> </dd>
                    </dl>
                </div>
                <div class="erp arp">
                    <dl>
                        <dt>TEL</dt>
                        <dd class="phonum"> <i class="icon"></i> <span class="word en"> <a href="tel:18980820575">18980820575</a> </span> </dd>
                    </dl>
                </div>
                <div class="erp crp">
                    <dl>
                        <dt>OTHER</dt>
                        <dd> <a class="word get-quote">获得报价与方案</a> </dd>
                        <dd> <a href="#" target="_blank" rel='nofollow' class="word" title="付款方式">付款方式</a> </dd>
                    </dl>
                </div>
                <div class="erp code-rp">
                    <dl>
                        <dt>Wechat</dt>
                        <dd class="code-wrap"> <span class="code"> <img src="/Public/Home/images/qr-code.jpg" alt="快上网微信公众号" /> </span> </dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>
    <div class="footerbot">
        <div class="friendlinks">
            <div class="wrap">
                <ul class="rpl">
                    <li><a href="http://www.ghjinhua.cn/" title="广汉锦华建材" target="_blank">广汉锦华建材</a></li><li><a href="http://www.scjiangan.com/" title="江安网站建设" target="_blank">江安网站建设</a></li><li><a href="http://chengdu.cdcxhl.cn/shop/
" title="成都微商城" target="_blank">成都微商城</a></li><li><a href="http://www.76089.cn/" title="成都翻译公司" target="_blank">成都翻译公司</a></li><li><a href="http://m.cdxwcx.com/tuoguan.html" title="电信服务器托管" target="_blank">电信服务器托管</a></li><li><a href="http://www.cdxwcx.cn/tuoguan/neijiang.html" title="电信内江机房" target="_blank">电信内江机房</a></li><li><a href="https://www.cdxwcx.com/tuiguang/zhenghe.html" title="整合营销" target="_blank">整合营销</a></li><li><a href="https://www.cdcxhl.com/idc/cqyd.html" title="重庆移动机房托管" target="_blank">重庆移动机房托管</a></li><li><a href="http://chengdu.cdcxhl.cn/
" title="成都网站建设" target="_blank">成都网站建设</a></li><li><a href="http://m.cdcxhl.cn/H5/
" title="H5建站" target="_blank">H5建站</a></li>                </ul>
            </div>
        </div>
        <div class="wrap">
            <div class="copyright"> <span class="en">©2007-2022</span> 成都快上网科技有限公司 <span class="en">ALL RIGHTS
                        RESERVED.</span> <a rel="nofollow" href="http://www.miitbeian.gov.cn" target="_blank">蜀ICP备19037934号</a> </div>
        </div>
    </div>
</footer>
<div class="fcwrap">
    <ul class="rpl clearfix">
        <li class="phone"> <a rel="nofollow" target="_blank" href="tel:18980820575"> <i class="icon"></i>
            <strong>18980820575</strong> </a> </li>
        <li class="qq"> <a rel="nofollow" target="_blank"
                           href="https://wpa.qq.com/msgrd?v=1&uin=244261566&site=qq&menu=yes"> <i class="icon"></i>
            <strong>244261566</strong> </a> </li>
        <li class="back-top"> <a href="javascript:void(0)" rel="nofollow" class="back-to-top"> <i class="icon"></i>
            <strong>回到顶部</strong> </a> </li>
    </ul>
</div>
<!--nav-->
<div class="n-Wrap">
    <div class="navBar visble show">
        <div class="barlogo">
            <a href="/" rel="nofollow">
                <img src="/Public/Home/images/logo1.png" alt="成都做网站" />
                <img src="/Public/Home/images/logo2.png" alt="成都网站设计" />
            </a>
        </div>
        <div class="bmenu">
            <i class="bar-top"><span></span></i>
            <i class="bar-cen"><span></span></i>
            <i class="bar-bom"><span></span></i>
            <i class="bar-left"><span></span></i>
            <i class="bar-right"><span></span></i>
        </div>
    </div>
    <section class="fixmenu">
        <div class="close-bar">
            <i class="bar-left"><span></span></i>
            <i class="bar-right"><span></span></i>
        </div>
        <nav class="smph">
            <ul>
                <li class="index-hrefs on"><a href="http://www.cdweb.net/"><font>首页</font></a></li>
                <li><a href="/about/" rel="nofollow"><font>关于快上网</font></a></li>
                <li><a href="/service/" rel="nofollow"><font>服务范围</font></a></li>
                <li><a href="/case/" rel="nofollow"><font>案例展示</font></a></li>
                <li><a href="/solve/" rel="nofollow"><font>解决方案</font></a></li>
                <li><a href="/news/" rel="nofollow"><font>建站资讯</font></a></li>
                <li><a href="/contact/" rel="nofollow"><font>联系快上网</font></a></li>
            </ul>
            <div class="pwrap">
                <span class="label">服务热线</span>
                <strong class="phone"><a href="tel:18980820575">18980820575</a></strong>
            </div>
        </nav>
    </section>
</div>
<!--end nav-->
<script src="/Public/Home/js/hotcss.js"></script>
<script type="text/javascript" src="/Public/Home/js/su_new.js"></script>
</body>
</html>
<script>
    $(".con img").each(function(){
        var src = $(this).attr("src");    //获取图片地址
        var str=new RegExp("http");
        var result=str.test(src);
        if(result==false){
            var url = "https://www.cdcxhl.com"+src;    //绝对路径
            $(this).attr("src",url);
        }
    });
    window.onload=function(){
        document.oncontextmenu=function(){
            return false;
        }
    }
</script>