If those are static files, just link to it directly. All decent servletcontainers/appservers have a well-developed DefaultServlet
. If those are static files located outside the webapplication from where you'd link them to, then you can also just add the root folder of those files as another context. It's unclear which server you're using, but if it were Tomcat, you could just add a new <Context>
to server.xml
:
<Context docBase="/path/to/static/files" path="/files" />
This way it's accessible by http://example.com/files/...
.
If those are dynamically generated files or files coming from a database, then you need to develop a servlet which does the IO job efficiently: i.e. do not unnecessarily store the entire data in memory (e.g. in a ByteArrayInputStream
or byte[]
before emitting them to the output. Just write the bytes immediately to the output as it comes in. You may find this those examples of a basic fileservlet and a more advanced fileservlet (supporting resumes and so on) useful.