Artur  Arsalanov Artur Arsalanov - 2 years ago 84
Java Question

Dynamically save files in web application

I am working on simple servlet project(on eclipse).
The code works with temporary directory and tmp files.
My directory for temp files is called "tmp" and it's on WebContent directory of my project.

I want to know - how can I get the path of a file in "tmp" directory (then I will set it as attribute and forward it to jsp which will be responsible for output).

when I open static files from that folder - no problem.
But the files created dynamically - it says like:
The requested resource (/ASCIIART/tmp/res5252907236669575469.txt) is not available

Answer Source

Actually, You shouldn't do that. I'll prefer a location on file system outside the war archive.

The reason is if server will restart(it may be frequent or occasionally) or you undeployed your application, all files in the tmp really will be gone. That's why you should use file system location like C:\appname\files.

The difficulty with this method is your requests for accessing these files can't be entertained automatically, be container/server.

But the upside of this approach is default security, i.e. no one can access files using a simple get request.

You can you BalusC file Servlet to fetch files from local file systems using simple get requests.

Also, you can save the new file path into database and read the file from that path whenever required.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download