Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Stefan Renemeister
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You can do something like this ::

 
Stefan Renemeister
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stefan

Try below code



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

 
Stefan Renemeister
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34680
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic