• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying image from a BLOB stored in DB

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic