Marc,

If you have performance problems, here is a suggestion:

Keep an index in your application scope that has the totals (since
load-time) of how many times each item has been displayed.  Then, keep
copies of the top 50 on-disk.  That way, if the image is one of your top-50,
you don't need the database but can simply open the disk file and read/print
it for speed on your most-used ones and only remove the temp files you made
for the less frequently used ones.

Regards,
David

-----Original Message-----
From: Marc AMIR-TAHMASSEB [mailto:[EMAIL PROTECTED]
Sent: Wednesday, November 12, 2003 9:59 AM
To: Struts Users Mailing List
Subject: Re: Dynamic Image from DB to JSP


Dear Hubert and David,
thanks for your help. Finaly i did like this :

in my jsp  a create a image tag as :
<img src="loadImage.do?id=<%=Integer.toString(image.getId())%>" >

loadImage.do create first a Java Temporary File (File.createTempFile)
containing the bytes of the image comming from the DB. Then it generates
a "document" with the appropriate content type (that i stored before)
and i push the image bytes[] to the response like this :

response.setContentType("image/pjpeg");
response.setHeader("Content-Disposition", "filename="+img.getName());
response.setHeader("Cache-Control", "no-cache");
response.setContentLength((int)img.length());

OutputStream ou = response.getOutputStream();
FileInputStream in = new FileInputStream(img);
byte[] buffer = new byte[(int)img.length()];
in.read(buffer);
ou.write(buffer);
ou.flush();
ou.close();
in.close();
img.delete();
img.deleteOnExit();

Regards,
Marc

David Friedman wrote:

>Marc,
>
>That's a tricky one I've planned for but haven't started yet.  My research
>showed the html:img tag should be used with a path.  That path can invoke
an
>action (direct, not tiled) that sets the content-type to the right type of
>image then tosses out the binary stream. So, it should be something like
>this:
>
>html:write get a URL such as "/images.do?id=555" or
>"http://somehost/images.do?id=555";.  Then your map an action for "/images"
>to set the content type, open the database, and print the binary stream
>appropriately.  Sounds a bit list an advertising program, almost. :)
>
>If something else works for you, please let me know as this is type of
image
>display is on my to-do list.
>
>Regards,
>David
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to