码迷,mamicode.com
首页 > 其他好文 > 详细

Clob和Blob的辅助工具

时间:2017-05-21 13:54:26      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:null   bin   doc   isnull   cte   class   instr   int   rac   

 1 /**
 2  * <p>Blob类型辅助工具类</p>
 3  */
 4 public class BlobUtil {
 5     
 6     /**
 7      * file 对象转换成blob
 8      * @param file
 9      * @return Blob
10      */
11     public static Blob fileToBlob(File file) {
12         try {
13             InputStream is = new FileInputStream(file);
14             Blob blob = Hibernate.createBlob(is);
15             return blob;
16         } catch (FileNotFoundException e) {
17             e.printStackTrace();
18         }    catch (IOException e) {
19             e.printStackTrace();
20         }
21         return null;
22     }
23 
24     /**
25      * blob转string
26      * @param blob
27      * @return
28      */
29     public static String blobToString(Blob blob) {
30         String str = "";
31 
32         if (blob == null) {
33             return str;
34         }
35 
36         try {    
37             InputStream ins = blob.getBinaryStream();
38             byte[] c = new byte[(int) blob.length()];
39             ins.read(c);
40             str = new String(c);
41             ins.close();
42         } catch (SQLException e) {
43             e.printStackTrace();
44         } catch (IOException e) {
45             e.printStackTrace();
46         }
47         return str;
48     }
49 
50     /**
51      * string转blob
52      * @param str
53      * @return
54      */
55     public static Blob stringToBlob(String str) {
56         str = StrUtil.formatNull(str);
57         return Hibernate.createBlob(str.getBytes());
58     }
59     
60     /**
61      * 把Blob类型转换为byte数组类型
62      * 
63      * @param blob
64      * @return
65      */
66     public static byte[] blobToBytes(Blob blob) {
67         byte[] bytes = null;
68 
69         if (blob == null) {
70             return bytes;
71         }
72 
73         BufferedInputStream is = null;
74         try {
75             is = new BufferedInputStream(blob.getBinaryStream());
76             bytes = new byte[(int) blob.length()];
77             is.read(bytes, 0, bytes.length);
78         } catch (Exception e) {
79             e.printStackTrace();
80         } finally {
81             try {
82                 if(null!=is){
83                     is.close();
84                 }
85                 is = null;
86             } catch (IOException e) {
87                 e.printStackTrace();
88             }
89         }
90         return bytes;
91     }
92     
93 }
 1 /**
 2  * <p>Clob类辅助工具类</p>
 3  */
 4 public class ClobUtil {
 5     /**
 6      * Clob转成字符串
 7      * @param clob java.sql.Clob
 8      * @return String
 9      */
10     public static String clobToStr(Clob clob) {
11         if (clob == null) {
12             return "";
13         }
14         try {
15             Reader inStreamDoc = clob.getCharacterStream();
16             char[] tempDoc = new char[(int) clob.length()];
17             inStreamDoc.read(tempDoc);
18             inStreamDoc.close();
19             String retstr = new String(tempDoc);
20             return retstr;
21         } catch (IOException e) {
22             e.printStackTrace();
23         } catch (SQLException es) {
24             es.printStackTrace();
25         }
26 
27         return null;
28     }
29     
30     /**
31      * 字符串转成Clob
32      * 
33      * @param str
34      *            String
35      * @return Clob
36      */
37     public static Clob StrToClob(String str) {
38         Clob clob = null;
39         if(!StrUtil.isNull(str)){
40             try {
41                 clob = new SerialClob(str.toCharArray());
42             } catch (SerialException e) {
43                 e.printStackTrace();
44             } catch (SQLException e) {
45                 e.printStackTrace();
46             }
47         }
48         return clob;
49     }
50 
51 }

 

Clob和Blob的辅助工具

标签:null   bin   doc   isnull   cte   class   instr   int   rac   

原文地址:http://www.cnblogs.com/yujiwei/p/6884596.html

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