Hibernate 对数据库Blob图片的查询操作---页面显示图片,文件的输出

怎样使用hibernate上传和更新图片在我的另一篇文章有所介绍,请参看 http://blog.csdn.net/fhway/archive/2006/11/01/1359482.aspx 在这里讲的是如何查询得到结果的问题

1.在jsp页面里输出,通常的做法是建立一个servlet的方式,我要讲的也是这个方法;代码如下;

/**
 * 
 
*/

package  com.sclh.hibernate.servlet;

import  java.io.IOException;
import  java.io.InputStream;
import  java.io.OutputStream;
import  java.sql.Blob;

import  javax.servlet.RequestDispatcher;
import  javax.servlet.ServletException;
import  javax.servlet.ServletRequest;
import  javax.servlet.ServletResponse;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  javax.servlet.http.HttpSession;

import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;

import  com.sclh.common.ChineseString;
import  com.sclh.hibernate.HibernateSessionFactory;
import  com.sclh.hibernate.bean.Zp;
import  com.sclh.hibernate.service.ZpService;

/**
 * 
@author fuhw
 * 
 
*/

public   class  ShowImageServlet  extends  HttpServlet  {
    
private static final Log log = LogFactory.getLog(ShowImageServlet.class);

    String rybh 
= "";
    String zpxh 
= "";

    
public void service(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
System.out.println(
"ShowImageServlet come in!");
        HttpSession session 
= (HttpSession) request.getSession();
        request.setCharacterEncoding(
"ISO8859-1");

        rybh 
= ChineseString.chineseStr((String) request.getParameter("rybh"));
        zpxh 
= ChineseString.chineseStr((String) request.getParameter("zpxh"));
        Zp zp 
= new Zp();
        ZpService service 
= new ZpService();

        String id 
= service.queryZpIdByRybhAndZpxh(rybh, Integer.valueOf(zpxh));
        org.hibernate.Session hibernateSession 
= HibernateSessionFactory
                .getCurrentSession();

        
try {
            zp 
= (Zp) hibernateSession.load(Zp.class, Integer.valueOf(id));
            
if(!zp.getPic().equals(null)){
                Blob photo 
= zp.getPic();
                InputStream in 
= photo.getBinaryStream();
                OutputStream out 
= response.getOutputStream();
                
byte[] buf = new byte[1024];
                
int len;
                
while ((len = in.read(buf)) != -1{
                    out.write(buf, 
0, len);
                }

                in.close();
                out.close();
            }
else{
                
//可以展示的其他照片
            }

        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }


}

我要通过两个参数 rybh(人员编号)&zpxh(照片序号)得到一个该人员在数据库表t_zp中的存放id号.表结构见上面的链接文章.然后load一下这个Zp.class.其他的就是读数据流,传数据流了,在页面的里面我们只要在显示图片的地方写上
                                 < td  width ="32%"  bgcolor ="#FFFFFF" >< IMG  id =personpic
                                    
height =231  alt =""  src ="/ShowImageServlet?rybh=<%=rybh%>&zpxh=2"
                                width
=183  name =personpic ></ td >

哈哈,就可以了;

2.文件的输出

 

     public   void  queryZp(Integer id, String filePath)  {
        log.debug(
"queryZp  instance");
        Session session 
= null;
        
try {
            session 
= getSession();
            Zp zp 
= (Zp) session.load(Zp.class, id);
            System.out.println(zp.getId().toString());
            InputStream is 
= ((java.sql.Blob)zp.getPic()).getBinaryStream();
            OutputStream os 
= new FileOutputStream(filePath);
            
byte[] byteBuffer = new byte[10240];
            
int len;
            
while ((len = is.read(byteBuffer)) != -1{
                os.write(byteBuffer, 
0, len);
            }

            is.close();
            os.close();
        }
 catch (Exception e) {
            e.printStackTrace();
        }
 finally {
            
try {
                session.close();
            }
 catch (Exception e1) {
                e1.printStackTrace();
            }

        }

    }

你只要指定load的id&路径,哈哈,它就给你一个可以观赏Blob的芳容了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值