Devashish Dixit Devashish Dixit - 2 months ago 11
Java Question

Output an image file from a servlet

How to serve an image, stored on my hard drive, in a servlet?

For Example:

I have an image stored in path

and I want to serve this in a servlet with the URL

  • map a servlet to the /file url-pattern
  • read the file from disk
  • write it to response.getOutputStream()
  • set the Content-Type header to image/png (if it is only pngs)