Clob和Blob转换byte数组

Posted zdf159

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Clob和Blob转换byte数组相关的知识,希望对你有一定的参考价值。

一.Clob转化成byte数组

  public static byte[] clobToBytes(Clob clob) {  
        BufferedInputStream is = null;  
        try {  
            is = new BufferedInputStream(clob.getAsciiStream());  
            byte[] bytes = new byte[(int) clob.length()];  
            int len = bytes.length;  
            int offset = 0;  
            int read = 0;  
            while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {  
                offset += read;  
            }  
            return bytes;  
        } catch (Exception e) {
            try {
                is.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }  
            is = null; 
            return null;  
        } finally {  
            try {  
                is.close();  
                is = null;  
            } catch (IOException e) {  
                return null;  
            }  
        }  
    }

二. Blob转换byte数组

  public static byte[] blobToBytes(Blob blob) {  
        BufferedInputStream is = null;  
        try {  
            is = new BufferedInputStream(blob.getBinaryStream());  
            byte[] bytes = new byte[(int) blob.length()];  
            int len = bytes.length;  
            int offset = 0;  
            int read = 0;  
            while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {  
                offset += read;  
            }  
            return bytes;  
        } catch (Exception e) {
            try {
                is.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }  
            is = null; 
            return null;  
        } finally {  
            try {  
                is.close();  
                is = null;  
            } catch (IOException e) {  
                return null;  
            }  
        }  
    }

 

以上是关于Clob和Blob转换byte数组的主要内容,如果未能解决你的问题,请参考以下文章

hibernate 大对象类型的hibernate映射

Java类型相互转换byte[]类型,blob类型

java读取blob,clob转换为字符串

Hibernate向数据库存入BLOB和CLOB类型的数据

Hibernate的Annotation中实体BLOB CLOB类型的注解

mysql使用blob存储base64格式文件