This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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 Java Interview Guide this week in the Jobs Discussion 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

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 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
author & internet detective

Joined: May 26, 2003
Posts: 33132

Thanks for posting the solution.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Patrick Shea

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:
subject: difficulty storing BufferedImage in Oracle BLOB field
It's not a secret anymore!