Devashish Dixit Devashish Dixit - 4 months ago 24
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

'Images/button.png'
and I want to serve this in a servlet with the URL
file/button.png
.

Answer
  • 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)
Comments