数据库存储BloB格式图片,并从数据库中取出显示到页面中

BloB类型就是以二进制的形式把图片保存到数据库中。包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小上不同。

TinyBlob 最大 255字节      ( 1024Byte(字节)=1KB )

Blob 最大 65KB

MediumBlob 中等16M
LongBlob 最大 4G

我自己是以表单里选择上传文件的形式(图片也可以看作是一种文件)来把图片插入到数据库中的。

以下是代码:

public class Info {
               private File photo;
	       private Date datetime;
		public File getPhoto() {
			return photo;
		}
		public void setPhoto(File photo) {
			this.photo = photo;
		}
               public Date getDatetime() {
			return datetime;
		}
		public void setDatetime(Date datetime) {
			this.datetime = datetime;
		}

public class infoDao {
	//把Found数据放入到数据库中
      public int InsertFound(Info info){
    	  PreparedStatement pst=null;
    	  int rs=0;
    	  DBcoon coon=new DBcoon();
    	  Connection coona=coon.getCoon();//连接数据库
    	  String sql_insert="insert into found(title,name,photo,datetime) values (?,?,?,?)";//sql语句
    	  
    	  try {
			pst=coona.prepareStatement(sql_insert);
			pst.setString(1, info.getTitle());
			pst.setString(2, info.getName());
			
	      
			InputStream str=new FileInputStream(info.getPhoto());//把图片放进数据库中
			long length=(long)str.available();//来获取文件的大小
			pst.setBinaryStream(3, str, length);//public final void setBinaryStream(int parameterIndex,java.io.InputStream x,long length)

			
                    DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //规定日期格式ʽ2005-8-8 9:17:42 
		    String datetime=mediumDateFormat.format(new Date());// new Date()获取系统时间
		  
			pst.setString(4, datetime);
		
			rs=pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 return rs;  
      }

根据title可以查询到需要显示的图片的记录,然后获取图片

InputStream is=rst.getBinaryStream("photo");//rst就是结果集
		        session.setAttribute("rst",is);//把结果放到session中


有一个可以形成图片的servlet或jsp,我是拿jsp写的。名字为images.jsp

<%@ page import="java.sql.*" %>
 <%@ page import="java.io.*" %>
 <%@ page import="java.util.*" %>
<% 
    InputStream is = (InputStream)session.getAttribute("rst");
     OutputStream os=null;
     response.setContentType("text/html");
     os = response.getOutputStream();
     
     byte[] buffer=new byte[1024];//一次传的文件量
		int len=0;
		try {
			while((len=is.read(buffer))>0){//读
				os.write(buffer, 0, len);//写 (,起始位置,长度)
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
	}      //以下两行代码不可少,会显示不出图片 原因为 jsp中 response.getWriter()和response.getOutputStream()相互冲突
                out.clear();
                out=pageContext.pushBody();
%>


然后在你需要显示图片的页面中写

<img width="300" height="300" src="images.jsp">

这就完成了。总体思想是要想获取一张图片,必须要根据该图片的特征(就是我文中提到的title)在数据库中查找出有该图片的记录,然后再获取它在数据库中的二进制,最后用OutputStream形成图片。
那么如果全部查询(select * from found),想把每一张图片都显示在同一页面,又该怎么办?看下一篇文章。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涛涛之海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值