aspose file tools*
The moose likes Oracle/OAS and the fly likes difficulty storing BufferedImage in Oracle BLOB field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Products » Oracle/OAS
Bookmark "difficulty storing BufferedImage in Oracle BLOB field" Watch "difficulty storing BufferedImage in Oracle BLOB field" New topic
Author

difficulty storing BufferedImage in Oracle BLOB field

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I have had a ton of trouble figuring this out, and I've read so many tutorials and countless forum posts, but I'm convinced there's a good way to do this. We dynamically generate a BufferedImage in memory, and originally we wrote them out to a flat file (both a PNG and a JPEG version of the same image), but now that we're moving to Oracle, we want to write them straight into the database.

The problem is, when you use ImageIO to write the BufferedImage into a particular format (e.g. PNG or JPEG), you must send it to an OutputStream. When you want to insert a BLOB into the database using a PreparedStatement, you can use the setBinaryStream method that takes an InputStream and a byte count (int) of the image. I've been trying to figure out how to get the OutputStream that ImageIO writes to channeled into the InputStream that setBinaryStream takes in. I've researched PipedInputStream & PipedOutputStream and tried to use them, but always got broken pipes, either because of my incorrect use of them or because PreparedStatement didn't know how to read the stream properly since the ends of the pipe are in different threads.

It's really annoying because writing the image out to the filesystem generally takes about as much time as everything else together (generating the image, reading it in from file, and storing it in the db). Isn't there a better/easier way?
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Finally...an easy way...

Let ImageIO's write method write out the BufferedImage to a ByteArrayOutputStream, then call that class's toByteArray() method and initialize a ByteArrayInputStream with that object. Then the PreparedStatement's setBinaryStream method will have an InputStream...

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29233
    
136

Stephen,
Thanks for posting the solution.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Patrick Shea
Greenhorn

Joined: May 09, 2011
Posts: 2
7 years later, thanks for the solution! I was googling for this solution today. It worked like a charm and made so much sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difficulty storing BufferedImage in Oracle BLOB field
 
Similar Threads
Error in Dowlnoading
basic, I think: get InputStream from OutputStream for image
uploading a word doc/excel files
Creating image file after manipulation in bits of image
Inserting Image into Oracle.