码迷,mamicode.com
首页 > 编程语言 > 详细

java blob存取图片

时间:2014-04-30 17:27:01      阅读:476      评论:0      收藏:0      [点我收藏+]

标签:com   http   blog   style   class   div   img   code   java   c   log   

1. 存入图片

bubuko.com,布布扣
        Connection con=db.conn;
        PreparedStatement pstmt;
        //File file = upload;
        FileInputStream inputImage = new FileInputStream(upload);
        byte[] buf = new byte[inputImage.available()];
        inputImage.read(buf);
        con.setAutoCommit(false);
        System.out.println("**before sql**");
        try {
            pstmt = con.prepareStatement("insert into zzbj_rs_region_detail(id,region_Id,coordinate1,coordinate2,coordinate3,table_Name,rs_id,icon,createTime) values("
                            + rdb.getId()+ ","+ rdb.getRegionId()+ ","+ rdb.getCoor1()+ ","+ rdb.getCoor2()+ ","+ rdb.getCoor3()+ ",‘"
                            + rdb.getTableName()+ "‘,"+ rdb.getRsId() + ",empty_blob(),sysdate)");//先存入空blob 再修改,可以存入大的图片,首次插入有大小限制,小图片没问题,大了就会报异常
            pstmt.executeUpdate();
            System.out.println("**afer sql**");
            ResultSet rs = pstmt.executeQuery("SELECT icon FROM zzbj_rs_region_detail where id="+ rdb.getId() + " for update");
            if (rs.next()) {
                oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("icon");
                OutputStream out = blob.getBinaryOutputStream();
                out.write(buf);
                out.flush();
                out.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        con.commit();
bubuko.com,布布扣

2. 修改图片

bubuko.com,布布扣
 1 FileInputStream inputImage = new FileInputStream(upload);
 2                 byte[] buf = new byte[inputImage.available()];
 3                 inputImage.read(buf);
 4                 con.setAutoCommit(false);
 5                 PreparedStatement pst=con.prepareStatement("SELECT icon FROM zzbj_rs_region_detail where id="+ rdb.getId() + " for update");
 6                 ResultSet rs = pst.executeQuery();
 7                 if (rs.next()) {
 8                     oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("icon");
 9                     OutputStream out = blob.getBinaryOutputStream();
10                     out.write(buf);
11                     out.flush();
12                     out.close();
13                 }
14                  con.commit();
bubuko.com,布布扣

3.  读取图片

bubuko.com,布布扣
        Connection con=db.conn;
        Statement stmt = con.createStatement();
        String sql = "select background from zzbj_rs_region where id=‘"+id+"‘ ";
        ResultSet rs =db.query(sql);
          if (rs.next()){
           oracle.sql.BLOB b = (oracle.sql.BLOB) rs.getBlob("background");
           long size = b.length();
           byte[] bs = b.getBytes(1, (int) size);
           res.setContentType("image/jpeg");
           OutputStream outs = res.getOutputStream();
          if(size>1){
           outs.write(bs);      
           outs.flush();}          
           db.closeDb();
           }
bubuko.com,布布扣

 

java blob存取图片,布布扣,bubuko.com

java blob存取图片

标签:com   http   blog   style   class   div   img   code   java   c   log   

原文地址:http://www.cnblogs.com/sjzq/p/3699149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!