重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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://cqcxhl.com/article/ipojjo.html">http://cqcxhl.com/article/ipojjo.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/dcgsijo.html">php时间戳查询数据库 php时间戳转换中国标准时间</a>
                </li><li>
                    <a href="/article/dcgsigj.html">阿里云ecs服务器能用多少网站 阿里云服务器ecs是一种简单高效</a>
                </li><li>
                    <a href="/article/dcgsijg.html">设置路由器提高网速 如何设置路由器可以提高网速</a>
                </li><li>
                    <a href="/article/dcgsiih.html">go语言node go语言圣经</a>
                </li><li>
                    <a href="/article/dcgsigd.html">Go语言ios go语言io操作</a>
                </li>        </ul>
    </div>
</div>
<div class="footer">
    <div class="footer_content">
        <div class="footer_content_top clear">
            <div class="content_top_share fl">
                <div><img src="/Public/Home/img/logo.png"></div>
                <div class="top_share_content">
                    <dd>分享至:</dd>
                    <dt class="bdsharebuttonbox clear" id="share">
                        <a href="#" class="bds_tsina iconfont fl" data-cmd="tsina" title="分享到新浪微博"></a>
                        <a href="#" class="bds_sqq iconfont fl" data-cmd="sqq" title="分享到QQ好友"></a>
                        <a href="#" class="bds_weixin iconfont fl" data-cmd="weixin" title="分享到微信"></a>
                        <a href="#" class="bds_weixin iconfont fl" data-cmd="tieba" title="分享到贴吧"></a>
                    </dt>
                    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
                </div>
            </div>
            <div class="content_top_left fl clear">
                <div class="top_left_list fl">
                    <dd><a href="/about/">关于我们</a></dd>
                    <dt>
                        <a href="/about/#gsjj">公司简介</a>
                        <a href="/about/#fzlc">发展历程</a>
                    </dt>
                </div>
                <div class="top_left_list fl">
                    <dd><a href="/service/">服务项目</a></dd>
                    <dt>
                        <a href="/service/">高端网站建设</a>
                        <a href="/miniprogram/">小程序开发</a>
                        <a href="/service/app.html">APP开发</a>
                        <a href="/service/yingxiao.html">网络营销</a>
                    </dt>
                </div>
                <div class="top_left_list fl">
                    <dd><a href="/jianzhan/">建站知识</a></dd>
                    <dt>
                        <a href="/jianzhan/2.html">网站建设</a>
                        <a href="/jianzhan/3.html">网站设计</a>
                        <a href="/jianzhan/4.html">网站制作</a>
                        <a href="/jianzhan/5.html">小程序</a>
                    </dt>
                </div>
                <div class="top_left_list fl">
                    <dd><a href="/contact/">联系我们</a></dd>
                    <dt>
                        <a href="/contact/#lxwm">公司地址</a>
                        <a href="/contact/#rczp">人才招聘</a>
                    </dt>
                </div>
            </div>
            <div class="content_top_right addressR fr">
                <div class="top_right_title addressf_title">
                    <a href="javascript:;" class="on">成都</a>
                    <a href="javascript:;">重庆</a>
                </div>
                <div class="top_right_content addressf">
                    <div class="right_content_li on">
                        <div class="right_content_list clear">
                            <dd class="fl iconfont"></dd>
                            <dt class="fl">电话:028-86922220</dt>
                        </div>
                        <div class="right_content_list clear">
                            <dd class="fl iconfont"></dd>
                            <dt class="fl">地址:成都市太升南路288号锦天国际A幢1002号</dt>
                        </div>
                    </div>
                    <div class="right_content_li">
                        <div class="right_content_list clear">
                            <dd class="fl iconfont"></dd>
                            <dt class="fl">电话:028-86922220</dt>
                        </div>
                        <div class="right_content_list clear">
                            <dd class="fl iconfont"></dd>
                            <dt class="fl">地址:重庆市南岸区弹子石腾龙大道58号2栋21-6</dt>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="footer_content_copyright clear">版权所有:成都创新互联科技有限公司重庆分公司
        <a href="http://beian.miit.gov.cn/" rel="nofollow" target="_blank">渝ICP备2021005571号-4</a>
    </div>
</div>

<!--浮窗-->
<div class="FloatingWindow clear">
    <a href="tencent://message/?uin=1683211881&Site=&Menu=yes" class="FloatingWindow_list fr">
        <div class="FloatingWindow_list_title">
            <dd class="iconfont"></dd>
            <dt><span>在线</span>咨询</dt>
        </div>
    </a>
    <a href="javascript:;" class="FloatingWindow_list fr">
        <div class="FloatingWindow_list_title">
            <dd class="iconfont"></dd>
            <dt>服务热线</dt>
        </div>
        <div class="FloatingWindow_list_down fadeInRight animated">服务热线:028-86922220</div>
    </a>
    <a href="javascript:;" class="FloatingWindow_list fr STop">
        <div class="FloatingWindow_list_title">
            <dd class="iconfont"></dd>
            <dt>TOP</dt>
        </div>
    </a>
</div>
<script src="/Public/Home/js/jquery-1.8.3.min.js"></script>
<script src="/Public/Home/js/comm.js"></script>
<script src="/Public/Home/js/wow.js"></script>
<script src="/Public/Home/js/common.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);
        }
    });
</script>