I want to access files from a servlet using getSystemResource. These files reside inside the project itself and on the hard disk. For the files inside the project I used:
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/testing.txt");
It works if I place the files in build/web folder, but when I clean and build the project all of the files are deleted. Where should I put the files?
For files outside the project, I use File object:
File file = new File("c://tmp//testing.txt");
InputStream is= new FileInputStream(file);
Is this a good practice?
Those files should not be deleted, unless you created them yourself inside the expanded WAR while the app is still running. Then those files will indeed be deleted when you redeploy the WAR, simply because they are not included in the original WAR.
The normal practice is to store those files in a fixed path outside the webapp context. You should definitely not use the tmp/temp folder for this. That folder is eligible for periodic cleanup by the underlying platform. Use for example
/var/webapp/upload. Document it properly so that the serveradmin will create it beforehand. Make it if necessary configureable by someweb.xmlparam.Or when the environment disallows creating folders and/or writing to the disk, then your last best bet is storing those files in a SQL database.