• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difficulty storing BufferedImage in Oracle BLOB field

 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephen,
Thanks for posting the solution.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
7 years later, thanks for the solution! I was googling for this solution today. It worked like a charm and made so much sense.
 
We noticed he had no friends. So we gave him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic