首页 > 代码库 > Clob和Blob的辅助工具
Clob和Blob的辅助工具
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的辅助工具
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。