Mysql数据库中图片字段Blob类型和String类型相互转换

1、Blob-->String

            String result = "";
            if (blob != null) {
                InputStream is = blob.getBinaryStream();
                ByteArrayInputStream bais = (ByteArrayInputStream) is;
                byte[] byte_data = new byte[bais.available()]; // bais.available()返回此输入流的字节数

                bais.read(byte_data, 0, byte_data.length);// 将输入流中的内容读到指定的数组
                BASE64Encoder encoder = new sun.misc.BASE64Encoder();          
                result = encoder.encodeBuffer(byte_data).trim();
                is.close();
            }

 

2、String-->Blob

          Blob blob = null;
  BASE64Decoder decoder = new sun.misc.BASE64Decoder();   
byte[] bytes1 = decoder.decodeBuffer(base64String);   ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);   blob = Hibernate.createBlob(bais);

 

posted @ 2016-03-24 09:43  指尖飞舞  阅读(7494)  评论(0编辑  收藏  举报