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>
METHOD =POST ACTION ="testimage.jsp" >
新 闻 标 题: TYPE ="text" NAME ="content" >
新 闻 图 片: TYPE ="file" NAME ="image" >
新闻内容:
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>
TYPE ="submit" > 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.*" %>
< %
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//加载驱动程序类
Connection con =DriverManager .getConnection("jdbc:odbc:denglu","sa","sa");
//建立数据库联机,其中denglu为数据库名,sa为连接数据库的帐号及密码。
Statement stmt =con .createStatement();
//建立Statement对象
String content =request .getParameter("content");
content =new String(content.getBytes("8859_1"),"gb2312");
String filename =request .getParameter("image");
filename =new String(filename.getBytes("8859_1"),"gb2312");
String detail =request .getParameter("txtmail");
detail =new String(detail.getBytes("8859_1"),"gb2312");
//获得所要显示图片的标题、存储路径、内容,并进行中文编码
FileInputStream str =new FileInputStream(filename);
String sql ="insert into picturenews(content,image,detail) values(?,?,?)" ;
PreparedStatement pstmt =con .prepareStatement(sql);
pstmt.setString(1,content);
pstmt.setBinaryStream(2,str,str.available());
pstmt.setString(3,detail);
pstmt.execute();
//将数据存入数据库
out.println("Success,You Have Insert an Image Successfully");
%>
4、网页中动态显示图片
接下来我们要编程从数据库中取出图片,其代码如下所示。
< %@ page contentType ="text/html;charset=gb2312" %>
< %@ page import ="java.sql.*" %>
< %@ page import ="java.util.*" %>
< %@ page import ="java.text.*" %>
< %@ page import ="java.io.*" %>
< %
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>
将此文件保存为testimageout.jsp文件。下一步要做的工作就是使用HTML标记:
src ="testimageout.jsp?id=< %=rs.getInt("id")%>" width =100 height =100 >
取出所要显示的图片,其中id是所要取出图片的编号。本例中我们输出了***个和***一个图片信息,详细的程序代码如下所示。
< %@ page contentType ="text/html;charset=gb2312" %>
< %@ page import ="java.sql.*" %>
动态显示数据库图片 title>
head>
< %
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();
//将指针移至***一条记录
%>
height =99 src ="testimageout.jsp?id=1" width =136 > td>
//取出***个图片
height =99 src ="testimageout.jsp?id=< %=rs.getInt("id")%>" width =136 > td>
//取出***一个图片
tr> table>
body>
html>
以上基于JSP实现数据库中图片的存储与显示的WEB应用程序在Windows 2000 Professional/SQL Server 2000/ Apache Tomcat 4.0/JDK 1.4 JAVA环境下调试通过。
本文标题:基于JSP实现数据库中图片的存储与显示
链接分享:http://chengdu.cdxwcx.cn/article/coecchh.html
10年专业互联网服务经验
成都高端建站设计团队
资深行业分析策划
B2C营销型网站建设者
前沿视觉设计、研发能力
前端代码深度符合SEO优化
成都市高新技术企业证书
具有完备的项目管理
完善的售后服务体系
深厚的网络运营经验
时刻新技术研发能力
16个网站系统软件著作权
甜橘子网站设计,为客户量身定制各类网站建设业务,包括企业型、电子商务型、行业门户型、品牌建立型等各类网站,实战经验丰富,成功案例众多。以客户利益为出发点,甜橘子网站制作为客户规划、定制符合企业需求、带有营销价值的建站方案,提供从网站前期定位分析策划到网站界面设计...
基本
文件
流程
错误
SQL
调试
请求信息 : 2026-09-04 16:49:46 HTTP/1.1 GET : /article/coecchh.html 运行时间 : 0.0276s ( Load:0.0026s Init:0.0012s Exec:0.0199s Template:0.0039s ) 吞吐率 : 36.23req/s 内存开销 : 245.63 kb 查询信息 : 7 queries 5 writes 文件加载 : 36 缓存信息 : 0 gets 1 writes 配置加载 : 129 会话信息 : SESSION_ID=1mk2ftpopj23l9khvrgdt6tot1
D:\wwwroot\hd19\wwwroot\index.php ( 1.12 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\ThinkPHP.php ( 4.61 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Think.class.php ( 12.26 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Storage.class.php ( 1.37 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Storage\Driver\File.class.php ( 3.52 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Mode\common.php ( 2.82 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Common\functions.php ( 53.56 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Hook.class.php ( 4.01 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\App.class.php ( 13.49 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Dispatcher.class.php ( 14.79 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Route.class.php ( 13.36 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Controller.class.php ( 11.23 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\View.class.php ( 7.59 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Behavior\BuildLiteBehavior.class.php ( 3.68 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Behavior\ParseTemplateBehavior.class.php ( 3.88 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Behavior\ContentReplaceBehavior.class.php ( 1.91 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Conf\convention.php ( 11.15 KB ) D:\wwwroot\hd19\wwwroot\App\Common\Conf\config.php ( 1.78 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Lang\zh-cn.php ( 2.55 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Conf\debug.php ( 1.50 KB ) D:\wwwroot\hd19\wwwroot\App\Home\Conf\config.php ( 0.38 KB ) D:\wwwroot\hd19\wwwroot\App\Home\Common\function.php ( 3.33 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Behavior\ReadHtmlCacheBehavior.class.php ( 5.62 KB ) D:\wwwroot\hd19\wwwroot\App\Home\Controller\ArticleController.class.php ( 5.69 KB ) D:\wwwroot\hd19\wwwroot\App\Home\Controller\CommController.class.php ( 0.14 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Model.class.php ( 60.11 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Db.class.php ( 32.43 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Db\Driver\Pdo.class.php ( 16.74 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Cache.class.php ( 3.83 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Cache\Driver\File.class.php ( 5.87 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Template.class.php ( 28.16 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Template\TagLib\Cx.class.php ( 22.40 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Template\TagLib.class.php ( 9.16 KB ) D:\wwwroot\hd19\wwwroot\App\Runtime\Cache\Home\7540f392f42b28b481b30614275e4e55.php ( 11.02 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Behavior\WriteHtmlCacheBehavior.class.php ( 0.97 KB ) D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Behavior\ShowPageTraceBehavior.class.php ( 5.24 KB )
[ app_init ] --START-- Run Behavior\BuildLiteBehavior [ RunTime:0.000008s ] [ app_init ] --END-- [ RunTime:0.000037s ] [ app_begin ] --START-- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000141s ] [ app_begin ] --END-- [ RunTime:0.000172s ] [ view_parse ] --START-- [ template_filter ] --START-- Run Behavior\ContentReplaceBehavior [ RunTime:0.000099s ] [ template_filter ] --END-- [ RunTime:0.000127s ] Run Behavior\ParseTemplateBehavior [ RunTime:0.002863s ] [ view_parse ] --END-- [ RunTime:0.002896s ] [ view_filter ] --START-- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000151s ] [ view_filter ] --END-- [ RunTime:0.000194s ] [ app_end ] --START--
1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= ) [8] Undefined index: pid D:\wwwroot\hd19\wwwroot\App\Home\Controller\ArticleController.class.php 第 47 行. [2] mkdir(): Permission denied D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Cache\Driver\File.class.php 第 59 行. [2] mkdir(): Permission denied D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Cache\Driver\File.class.php 第 59 行. [2] file_put_contents(./App/Runtime/Temp/c/cf3a9018f5df8cac44f0aa42d0bca09d.php): failed to open stream: No such file or directory D:\wwwroot\hd19\wwwroot\ThinkPHP\Library\Think\Cache\Driver\File.class.php 第 132 行.
DESCRIBE qt_article [ RunTime:0.001157s ] SELECT * FROM `qt_article` WHERE ( name='coecchh' ) LIMIT 1 [ RunTime:0.002698s ] DESCRIBE cx_nav [ RunTime:0.000651s ] SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1 [ RunTime:0.000134s ] SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1 [ RunTime:0.000094s ] SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= ) [ RunTime:0.000081s ] SELECT `id`,`name`,`biaoji`,`title` FROM `qt_article` ORDER BY biaoji asc LIMIT 30 [ RunTime:0.002477s ] UPDATE `qt_article` SET `biaoji`=biaoji+1 WHERE ( id=1905041 ) [ RunTime:0.000985s ] UPDATE `qt_article` SET `biaoji`=biaoji+1 WHERE ( id=1905019 ) [ RunTime:0.001045s ] UPDATE `qt_article` SET `biaoji`=biaoji+1 WHERE ( id=1905052 ) [ RunTime:0.000986s ] UPDATE `qt_article` SET `biaoji`=biaoji+1 WHERE ( id=1904923 ) [ RunTime:0.001024s ] UPDATE `qt_article` SET `biaoji`=biaoji+1 WHERE ( id=1904977 ) [ RunTime:0.001051s ]
0.0276s