• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )

 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can do something like this ::

 
Stefan Renemeister
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author & internet detective
Posts: 41967
911
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

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.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic