Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes BLOB(image) -> ByteArrayOutputStream -> display on screen Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "BLOB(image) -> ByteArrayOutputStream -> display on screen" Watch "BLOB(image) -> ByteArrayOutputStream -> display on screen" New topic
Author

BLOB(image) -> ByteArrayOutputStream -> display on screen

Stefan Renemeister
Greenhorn

Joined: Sep 26, 2009
Posts: 13
Hi all,

I'm fairly new to java, and as an exercise created a database (mysql), containing a table "photo_tbl" in which resides a couple pictures as BLOBs.
I created a java class, shown below that manages to read these images out of my database into a ByteArrayOutputStream.

My big question to u: How can I get the ByteArrayOutputStream to display as the original photo? (and not some freaky binary output )

ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi,

You can do something like this ::



Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
Stefan Renemeister
Greenhorn

Joined: Sep 26, 2009
Posts: 13
ujjwal soni wrote:Hi,

You can do something like this ::



Hi ujjwal soni,
thank you for your reply. Perhaps I don't understand you properly, or I am lacking knowledge here, but it seems that you want me to put a file on disk first?
In my current situation I have an application that delivers an ByteArrayOutputStream, containing the bytes of an image. How can I show that on a web page?
If I need to transform that to a file on disk, that's ok with me, but how?

Once again, thank you for your help.
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi Stefan

Try below code



Then, you can refer that image on jsp or html page like :

Stefan Renemeister
Greenhorn

Joined: Sep 26, 2009
Posts: 13
ujjwal soni wrote:Hi Stefan

Try below code



Then, you can refer that image on jsp or html page like :



Hi ujjwal,

allright, but where in my code do I need to add this? in the while I guess? If I do so, netbeans throws me a:
"Error non-static method writeTo(java.io.OutputStream) cannot be referenced from a static context"
What is this about?

And my second question would be: am I doing this in the proper way now? It seems like an awful long way just to show a picture from a database.
Stefan Renemeister
Greenhorn

Joined: Sep 26, 2009
Posts: 13
Hi Ujjwal,

I think I get it now, it works now, this is what it finally is:



Thanks a lot, you were of great help!
Only question that remains is: am I doing this proper? It seems so inefficient, having one output stream filling the other?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Stefan Renemeister wrote: It seems so inefficient, having one output stream filling the other?

Your code looks fine. The having one stream fill another is necessary. It disconnects the image from the database's blob/output stream and puts it in one that is more usable - the file.


[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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BLOB(image) -> ByteArrayOutputStream -> display on screen
 
Similar Threads
Problem inserting Blob into Oracle DB
Using getBlob(), got java.lang.AbstractMethodError
Reading From a Binary Large Object(BLOB)
Retrieving image from database - getBlob?
Inserting BLOB in Oracle with Studio 4..