File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Conversion Blob to Byte Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Conversion Blob to Byte Array" Watch "Conversion Blob to Byte Array" New topic
Author

Conversion Blob to Byte Array

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
I am trying to convert a Blob into a Byte array but sometimes when i run the application I get this exception

Blob may not be manipulated from creating session

this is the way i am converting Blob to byte Array



Is there another way to convert a java.sql.Blob into a Byte array
Parham Emami
Greenhorn

Joined: Jul 11, 2003
Posts: 15
Hi,
I've used bobs with the following piece of code. You could adapt it to what you require:

public void setBlob(PreparedStatement pstmt, int index, Object obj) throws DAOException, SQLException {
ByteArrayOutputStream b_out = new ByteArrayOutputStream();
try {
ObjectOutputStream o_out = new ObjectOutputStream(b_out);
o_out.writeObject(obj);
o_out.close();
} catch (IOException e) {
log.error(e);
throw new DAOException(e);
}
byte[] b = b_out.toByteArray();
ByteArrayInputStream b_in = new ByteArrayInputStream(b);


pstmt.setBinaryStream(index, b_in, b.length);
}

this method could be used by your insert or update method like this:
setBlob(pstmt1, 2, myObject);

Cheers
Parham
 
 
subject: Conversion Blob to Byte Array