Static and temporary files in servlets


Put not-to-be-web-accessible static files under WEB-INF. If you need a real path – e.g. to pass outside Java – and your container unpacks to a filesystem then get it as:


or, if stream access within Java is acceptable (this works even if the .war isn’t unpacked to a filesystem):


For temporary files, use:

File tempDir = (File) request.getServletContext().getAttribute(ServletContext.TEMPDIR);
File tempFile = File.createTempFile("prefix-", ".tmp", tempDir);

(thanks) (thanks)

Insert Photo



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: