Devashish Dixit Devashish Dixit - 1 year ago 102
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

Answer Source
  • 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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download