views:

146

answers:

1

I want to put my images to db but i want to have a link on each image. So I have the idea to copy the image to the temp dir of tomcat (or weblogic). But I must map the tempdir to a virtual path.

How do it ?

+1  A: 

Just create a servlet and in its doGet method serve a binary image content ( Content-Type: image/jpeg, or gif ).

Make sure to set the following fields in HttpServletResponse:

  • ETag
  • Last-Modified
  • Content-Type
  • Content-Length

Then retrun the image binary as a body of HttpServletResponse, write it directly to a stream obtained through HttpServletResponse.getOutputStream.

You can pass image ID to this servlet as a URL parameter, so you can serve different images through the same servlet. You may even cache images on disk, but because you have a servlet you will have an ability to setup a temp directory without virtual mapping.

EDIT

Actually you can create several virtual mappings to this servlet and in the servlet doGet method examine context path and serve the image based on that context path name.

Alexander Pogrebnyak
Thanks but for DB connection is probably not the best way if I want to display more than 1 files ?
@quilovnic: I don't understand your concern with DB connection. If db performance allows it, serve the images directly from the database, if you think you will benefit from caching, then cache them in the Servlet container ( temp directory on disk, or just in application memory if it does not grow too much ). Just be aware that read/write to the disk will introduce its own I/O bottleneck, and in some cases your application may just spend all its time waiting for disk I/O to finish.
Alexander Pogrebnyak
So, you think that the DB performance is not impacted if I open a great number of files from DB ? As you say, I would like create a cache. But if my files are big ... ?
@quilovnic. All I am saying is that you need to do performance testing either way, with or without cache and don't be surprised to see performance suffer because of disk I/O, especially if you store large files on disk. I only suggest that you have a servlet to present images to the clients. This makes storage abstraction much easier to accomplish, because at the end of the day your clients don't really care where the original images reside.
Alexander Pogrebnyak
@alexander great thanks for your help. I follow your advice