File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes how to convert byte[] to blob Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to convert byte[] to blob" Watch "how to convert byte[] to blob" New topic
Author

how to convert byte[] to blob

kalpana srinuvasan
Greenhorn

Joined: Jul 08, 2008
Posts: 9
i have to know whether conversion from Byte[] to blob is possible. if so , what will be the code??
please help me
thanks in advance .
Karthikeyan Ramaswamy
Ranch Hand

Joined: Apr 30, 2008
Posts: 41
hi,

try this ...............


//myByte is my byte array

java.sql.Blob blob=null;
blob.setBytes(1, myByte );


----------------------------------
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Hopefully there would in real life be something else between those two lines of code. As written, the second line will throw a NullPointerException.
kalpana srinuvasan
Greenhorn

Joined: Jul 08, 2008
Posts: 9
hi karthikeyan... thanks for your reply...
but i have to display that bolb image in the jsp page using struts ... can you help me for that .. please
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Displaying a blob via struts is a lot different than converting it. You may be trying to do too many things at once. In particular, try converting the blob to a readable image, then work on serving the image via struts.
[ July 10, 2008: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
kalpana srinuvasan
Greenhorn

Joined: Jul 08, 2008
Posts: 9
by using jsp , i can able to get the image as the byte[] using Formfile , i have to insert that image to my database (having the column datatype as "BLOB") and have to display in the jsp page after finished uploading ..
if you have solution for these issue . please reply..
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Perhaps you should try the struts forum. It sounds like you're not sure how to pass the data back/forth in struts. If you can save it to a database, reading it should be trivial (the JDBC component of it anyway).
[ July 10, 2008: Message edited by: Scott Selikoff ]
prakash jape
Greenhorn

Joined: Sep 17, 2008
Posts: 1
Blob blob=null; //is our blob object
byte[] buffer; //is our byte array
blob=new SerialBlob(buffer);
Vamsi Chada
Greenhorn

Joined: Aug 12, 2004
Posts: 10
The SerialBlob is a very good option on this
Ravi Choudhari
Greenhorn

Joined: Aug 03, 2010
Posts: 9
InputStream is = myBlob.getBinaryStream();

// IOUtils is from Apache Commons IO
byte[] a = IOUtils.toByteArray(is);
Ravi Choudhari
Greenhorn

Joined: Aug 03, 2010
Posts: 9
Blob blob = Hibernate.createBlob(bytes);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to convert byte[] to blob