免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1508 | 回复: 0
打印 上一主题 下一主题

java blob存取图片 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-07 13:50 |只看该作者 |倒序浏览
1. 存入图片
  1. Connection con=db.conn;
  2.         PreparedStatement pstmt;
  3.         //File file = upload;
  4.         FileInputStream inputImage = new FileInputStream(upload);
  5.         byte[] buf = new byte[inputImage.available()];
  6.         inputImage.read(buf);
  7.         con.setAutoCommit(false);
  8.         System.out.println("**before sql**");
  9.         try {
  10.             pstmt = con.prepareStatement("insert into zzbj_rs_region_detail(id,region_Id,coordinate1,coordinate2,coordinate3,table_Name,rs_id,icon,createTime) values("
  11.                             + rdb.getId()+ ","+ rdb.getRegionId()+ ","+ rdb.getCoor1()+ ","+ rdb.getCoor2()+ ","+ rdb.getCoor3()+ ",'"
  12.                             + rdb.getTableName()+ "',"+ rdb.getRsId() + ",empty_blob(),sysdate)");//先存入空blob 再修改,可以存入大的图片,首次插入有大小限制,小图片没问题,大了就会报异常
  13.             pstmt.executeUpdate();
  14.             System.out.println("**afer sql**");
  15.             ResultSet rs = pstmt.executeQuery("SELECT icon FROM zzbj_rs_region_detail where id="+ rdb.getId() + " for update");
  16.             if (rs.next()) {
  17.                 oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("icon");
  18.                 OutputStream out = blob.getBinaryOutputStream();
  19.                 out.write(buf);
  20.                 out.flush();
  21.                 out.close();
  22.             }
  23.         } catch (SQLException e) {
  24.             e.printStackTrace();
  25.         }
  26.         con.commit();
复制代码
2. 修改图片
  1. 1 FileInputStream inputImage = new FileInputStream(upload);
  2. 2                 byte[] buf = new byte[inputImage.available()];
  3. 3                 inputImage.read(buf);
  4. 4                 con.setAutoCommit(false);
  5. 5                 PreparedStatement pst=con.prepareStatement("SELECT icon FROM zzbj_rs_region_detail where id="+ rdb.getId() + " for update");
  6. 6                 ResultSet rs = pst.executeQuery();
  7. 7                 if (rs.next()) {
  8. 8                     oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("icon");
  9. 9                     OutputStream out = blob.getBinaryOutputStream();
  10. 10                     out.write(buf);
  11. 11                     out.flush();
  12. 12                     out.close();
  13. 13                 }
  14. 14                  con.commit();
复制代码
3.  读取图片
  1. Connection con=db.conn;
  2.         Statement stmt = con.createStatement();
  3.         String sql = "select background from zzbj_rs_region where id='"+id+"' ";
  4.         ResultSet rs =db.query(sql);
  5.           if (rs.next()){
  6.            oracle.sql.BLOB b = (oracle.sql.BLOB) rs.getBlob("background");
  7.            long size = b.length();
  8.            byte[] bs = b.getBytes(1, (int) size);
  9.            res.setContentType("image/jpeg");
  10.            OutputStream outs = res.getOutputStream();
  11.           if(size>1){
  12.            outs.write(bs);      
  13.            outs.flush();}         
  14.            db.closeDb();
  15.            }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP