Posted on 2007-11-29 17:35
G_G 閱讀(2109)
評論(2) 編輯 收藏 所屬分類:
javaGeneral
????
/*
?????*?將對象轉(zhuǎn)化成java.sql.Blob?
?????*?要求?對象是序列化的
?????
*/
????
public
?java.sql.Blob?ObjectToBlob(Object?obj)?
throws
?IOException{
????????
try
?{
????????????ByteArrayOutputStream?out?
=
?
new
?ByteArrayOutputStream();
????????????ObjectOutputStream?outputStream?
=
?
new
?ObjectOutputStream(out);
????????????outputStream.writeObject(obj);
????????????
byte
[]?bytes?
=
?out.toByteArray();
????????????outputStream.close();
????????????
return
?Hibernate.createBlob(bytes);
????????}?
catch
?(Exception?e)?{
????????????
//
?TODO:?handle?exception
????????????System.out.println(
"
ObjectToBlob
"
);
????????????
return
?
null
;
????????}????????
????}
????
????
????
/*
?????*?將java.sql.Blob?轉(zhuǎn)化成?對象?相應(yīng)對象
?????*?要求?對象是序列化的
?????
*/
????
????
public
?Object?BlobToObject(java.sql.Blob?desblob,Object?obj)?
throws
?IOException{
????????
try
?{
????????????ObjectInputStream?in?
=
?
new
?ObjectInputStream(desblob.getBinaryStream());
????????????obj?
=
??in.readObject();
????????????in.close();????
????????????
return
?obj;
????????}?
catch
?(Exception?e)?{
????????????
//
?TODO:?handle?exception
????????????System.out.println(
"
BlobToObject
"
);
????????????e.printStackTrace();
????????????
return
?
null
;
????????}????????
????}????
????
????