Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Loading an Image from the database and display it on browser

  Asked By: Boell    Date: Feb 23    Category: Java    Views: 5157

I do not know if this is even possible.

At the moment, to load an image inside an Html page you need to use the <img src=""> tag. and in the src you put the path of the image. Now I would like to save an image inside the database.

An option to still display the image on the browser would be that my service object, would load the object from the database (saved as blob) then save it somewhere on the Server, and the still use the <img> tag to load the image from that location.

However I was wondering wheather there is another way to do it without saving this image on the server. that is loading the bytes from the database (or a location on the server) and provided these bytes to the jsp page to display the image.

Is this possible? or?



6 Answers Found

Answer #1    Answered By: Fairuzah Alam     Answered On: Feb 23

Yes, it is possible with Component-Base Thechnology like JSF :
<h:graphicImage value="#{beanName.property}" />
The image  comes as Blob from DB.

Answer #2    Answered By: Gerardo Morgan     Answered On: Feb 23

If you can use struts in your app just let me know, so I can tell you the exact way.

Answer #3    Answered By: Kawakib Mansour     Answered On: Feb 23

I'm not sure if I understood the problem you've encountered correctly, but hope it helps,
you can use img tag  (or if ur using struts, html:img) and point the url of a servlet (or action if using struts) to a servlet (or action), load  the image  in servlet (action) and write it in the ServletOutputStream. The only thing you have to consider is that you should set the output of the servlet to image.
If this is what you want to do, I can send you the code (pure servlet or struts). Just let me know...

Answer #4    Answered By: Julia Hughes     Answered On: Feb 23

I can use struts in my app. Can you help me?

Answer #5    Answered By: Scarlett Hughes     Answered On: Feb 23

Although the mechanism is exactly the same in Servlets but I rather explain this how-to in Struts because of its html:img tag.

OK Pal
Here's the solution:
In order to download a photo from a DB and display  it right on your web page:

A) you need an Action, say: DownloadAction
B) your DownloadAction need no corresponding ActionForm
C) in the execute() method of DownloadAction:
1) get the OutputStream:
2) set its content type to "image/jpeg":
3) get your object  from DB (the object which this photo as one of its fields)
4) get the photo into a new blob  object
5) convert this Blob to a byte array
byte[] bytes  = null
bytes = blob.getBytes(1, (int) (blob.getLength()))
6) write it to the OutputStream

D) in your web page  use html:img tag  as follows:
<html:img src="downloadAction.do">

Answer #6    Answered By: Fe Fsrfewr     Answered On: Feb 25

how about this image program, will it be a solution for loading image without saving.