I have stored image files in a database. Is there any way to display that image in a web browser by querying the database?

Dieter Wimberger

I would recommend you to retrieve the image via JDBC from a simple HTTP servlet.

Things you have to take care about:

  • Ensure to set the correct Mime type. This has to be done calling HttpServletResponse.setContentType(String type);
    e.g. myHttpServletResponse.setContentType("image/jpeg");
  • Ensure that your RDBMS does not limit result sizes (i.e. check the manuals if you get half images, and always the same block sizes).
  • Attach ?<param>=<value> to your src URL to specify the picture to be retrieved. This param can be retrieved within your service method very simple, using:
    HttpServletRequest.getParameter(String name);
    The HTML tag for the image would then be something like follows:
    &lt;img src="http://www.mydomain.com/PictureServlet?id=35"&gt;
    (Sure you can use more params if you need to do so.)
  • Use some simple or sophisticated caching algorithm to limit your systems load.
Be sure to check the Servlet FAQ for questions on Servlets.
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact