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://jxjierui.cn/article/coecchh.html
基本
文件
流程
错误
SQL
调试
请求信息 : 2026-02-17 01:36:34 HTTP/1.1 GET : /article/coecchh.html 运行时间 : 0.0683s ( Load:0.0032s Init:0.0005s Exec:0.0602s Template:0.0044s ) 吞吐率 : 14.64req/s 内存开销 : 2,248.59 kb 查询信息 : 12 queries 5 writes 文件加载 : 36 缓存信息 : 0 gets 2 writes 配置加载 : 130 会话信息 : SESSION_ID=745604ed81ujr39d8eetkaacc3
/home/wwwroot/jxjierui.cn/index.php ( 1.12 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/ThinkPHP.php ( 4.61 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Mode/common.php ( 2.82 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Common/functions.php ( 53.56 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/App.class.php ( 13.49 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/View.class.php ( 7.59 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/convention.php ( 11.15 KB ) /home/wwwroot/jxjierui.cn/App/Common/Conf/config.php ( 2.12 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Lang/zh-cn.php ( 2.55 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/debug.php ( 1.48 KB ) /home/wwwroot/jxjierui.cn/App/Home/Conf/config.php ( 0.32 KB ) /home/wwwroot/jxjierui.cn/App/Home/Common/function.php ( 3.33 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB ) /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php ( 6.11 KB ) /home/wwwroot/jxjierui.cn/App/Home/Controller/CommController.class.php ( 1.60 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB ) /home/wwwroot/jxjierui.cn/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 13.96 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
[ app_init ] --START-- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ] [ app_init ] --END-- [ RunTime:0.000023s ] [ app_begin ] --START-- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000138s ] [ app_begin ] --END-- [ RunTime:0.000151s ] [ view_parse ] --START-- [ template_filter ] --START-- Run Behavior\ContentReplaceBehavior [ RunTime:0.000057s ] [ template_filter ] --END-- [ RunTime:0.000078s ] Run Behavior\ParseTemplateBehavior [ RunTime:0.003509s ] [ view_parse ] --END-- [ RunTime:0.003524s ] [ view_filter ] --START-- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000062s ] [ view_filter ] --END-- [ RunTime:0.000071s ] [ 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 /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php 第 47 行. [2] file_put_contents(./App/Runtime/Temp/cf3a9018f5df8cac44f0aa42d0bca09d.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行. [8] Undefined index: db_host /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 120 行. [8] Undefined index: db_port /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 121 行. [8] Undefined index: db_name /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 122 行. [2] file_put_contents(./App/Runtime/Temp/8238968dfab37202e1e3dd3ac46419a3.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.
0.0683s