Users can upload images on my website. My servlet needs to write the uploads into directory images of my webapp. For writing it onto the disk I need to create a File() object.
One way is that I can hardcode the complete path to something like
new File("/usr/tomcat/webapps/ROOT/images/file.jpg")
Is there a way so that I can specify something like new File("images/file.jpg").. Can I do it using new File (URI uri ) so that I do not have to hardcode the complete path. My servlet is located inside /usr/tomcat/webapps/ROOT/WEB-INF/classes/
The images folder is not a directory. It is actually a symbolic link which is pointing to another directory on the filesystem outside tomcat
Don’t store images under your webapp deployment directory: next time you’ll redeploy the app, you will lose all your images. And moreover, there is not always a directory for a webapp, since it’s typically deployed as a war file.
Store your images in an external location, at an absolute path, and configure Tomcat or another web server to serve images from this location, or implement a servlet which serves images from this location.
See Image Upload and Display in JSP for additional pointers.
EDIT:
If your problem is the hard-coded absolute path in the Java code, use a System property that you set when starting Tomcat, or a context param in the web.xml, or use a properties file to store the path, or your database.