• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Displaying image from a BLOB stored in DB

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

I store the bytes of an image inside a BLOB field in a database.
I want to be able to display this image beside the other text fields from the same table. One idea is to use a Servlet to deliver the mime type and then spit the bytes to the browser. But to do this, I would have to open a new window.

Since I don't want to serialize the BLOB to an image file; how can I achieve this ? I'm not sure how to use the HTML IMG tag to do this. Thanks

Pho
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What database are you using; Oracle, MySQL, something else?

BLOBs unfortunately don't work exactly the same on all databases. You'll find examples of how to work with BLOBs in Java if you search Google for "java oracle blob", for example.

Or do you already know how to read the BLOB, but you just want to know how to send it to the client from your web app?

Why do you think you would need to open a new window if you do it with a servlet?

Just include an IMG tag in your HTML page with an URL that points to the servlet that retrieves and sends out the image, instead of the URL of an image file.
[ June 13, 2006: Message edited by: Jesper Young ]
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using MySql5. My problem was actually sending it to the client. I will try your solution about pointing the img tag to the servlet. Thanks Jesper.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create the servlet you were talking about. Then inside the jsp use something like <img src=�/your_app/your_servlet?some_parameters�> it will show the image as any other non dynamic you have in your site. Also you can pass parameters in session. In fact the browser doesn�t knows it isn�t a static image, it only cares about the tag calling it and the mime type. Take into account that the image in the blob must be a web compliant, lets say it will be easier to show a jpg from the database than to show a bmp, cause not every browser knows how to deal with bmp.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic