Output an image file from a servlet

java, servlets

Solution

- 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)

Problem

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`.

Original source

Related problems