Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Boell Fischer   on Feb 23 In Java Category.

  
Question Answered By: Scarlett Hughes   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:
out=response.getOutputStream()
2) set its content type to "image/jpeg":
response.setContentType("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
out.write(bytes)

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

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 


Tagged: