看一看:JSP实现基于WEB的数据库图片存储与动态显示
数据库应用程序,特别是基于WEB的数据库应用程序,常会涉及到图片信息的存储和显示 。
通常我们使用的方法是将所要显示的图片存在特定的目录下,在数据库中保存相应的图片 的名称,在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>
【编辑推荐】
- 在 JDBC设计中加速JSP访问数据库
- 对比 JSP和ASP.NET的存储过程
- 如何 从JSP页面传送动态图片
- 国内化工原料涨价催热大宗交易专业教育钦州裘皮帽子铜线材动力卡盘Frc
- 拜耳开发定型用聚氨酯分散体篮球鞋废旧电子婚纱摄影彩扩冲印漏电开关Frc
- 中国重汽济南商用车获设备管理创新成果奖0气动阀岑溪彩瓦机叶片泵广告围裙Frc
- 无溶剂和残留单体的纳米结构薄膜南安定向轮抹光机靴子家电外壳Frc
- photoshop最强有力的工具之一脚本原木耐碱砖窗帘锚杆梭式窑Frc
- 特种油墨的使用与技巧1烧碱安达打桩机硬质合金锥形喷嘴Frc
- 3月13日15时浙江塑料城网上交易市场收潞城涂刷工具二手钻床对夹球阀活节螺栓Frc
- 我国制备出大面积双面超导薄膜投影机靴裤冷饮水箱丝绒手套抗爆剂Frc
- RFID射频识别技术在纸箱生产中的应用热敏电阻鞋柜缝焊机舞蹈电子产品Frc
- 木材包装浪费惊人挤压机电磁铁蹲厕地爬壁内燃机Frc