二维码写入到数据库

1,数据库的字段用BLOB接受  hibernate映射blob

2,存储二维码
[java]  view plain copy
  1.  public static Blob StorageTwoDimensionCode(TbAssets assets) throws IOException{  
  2.       
  3.     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();//字节数组流  
  4.     code.encoderQRCode(content, outputStream, "png",9);  
  5.       
  6.     //这里的Blob是java.sql包中的  
  7.     Blob codeimg =  Hibernate.createBlob(outputStream.toByteArray()); //outputStream转一个blob,这里可以接收InputStream和byte[]  
  8.     outputStream.flush();  
  9.     outputStream.close();  
  10.       
  11.     return codeimg; //返回一个blob  
  12. }  


3,如果要读出来,在页面显示

[java]  view plain copy
  1. public void showCodeImg(){  
  2.         TbAssets assets = assetsService.getAssetsById(asid);  
  3.         Blob codeimg = assets.getCodeImg();  
  4.         try {  
  5.             BufferedInputStream inputStream = new BufferedInputStream(codeimg.getBinaryStream()); //把BLOB数据转为二进制流  
  6.             BufferedOutputStream outputStream = new BufferedOutputStream(ServletActionContext.getResponse().getOutputStream());  
  7.             byte[] b = new byte[1024];  
  8.             int len = 0 ;  
  9.             while ((len=inputStream.read(b))!=-1) {  
  10.                 outputStream.write(b, 0, len);  
  11.             }  
  12.             outputStream.flush();  
  13.             inputStream.close();  
  14.             outputStream.close();  
  15.         } catch (Exception e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值