i want to create and save a file in inside my web application directory which is runninig on server. How can i do that? How do I get the absolute path of my project directory? My web server is apache tomcat.
Do you really want to save the file inside your web app? Really? That means that when you redeploy the app, you need to either wipe out the created files, or restore them in some fashion. That's fragile and a royal pain. Better to store the files external to the web app.
But to answer the question, check out ServletContext.getRealPath()
i cannot use ServletContext#getRealPath as it is creating other problems for me. Also it requires a file as a parameter where as in my case the file has to be created and is not existing currently.
Honsestly I would not like to keep the file in my web app folder but I am not sure if I would be allowed to write the file somewhere outside on a professional web server.
Secondly these files would be temporary and would be created on per request basis.
How is this problem solved in case of real world applications.. I mean what is the standard approach?
Such files are usually written to the system temp folder.
And saying things like "I cannot use the correct approach because I had problems" is self-defeating and won't get you much of anywhere. In this case it's moot as writing into the web app is the wrong approach, but in the future don't dismiss what might turn out to be the correct way of doing things just because you do not understand how to use it yet.
Joined: Mar 19, 2009
- I had read somewhere that ServletContext.getRealPath is not a reliable approach to follow (though i dont know why )
Secondly I need to give a virtual file name as a parameter to this method what should be that? any file that is present in my web app?
Thirdly I tried using it like this
but it gave me null pointer exception
please tell me if it is not the correct way..
Also the file that is creating the file on server is not a servlet so i'll have to pass this path as a parameter to that class whic I want to avoid.
Lastly I am new to web apps in java so please be easy on me
Bear Bibeault wrote:Do you really want to save the file inside your web app? Really? That means that when you redeploy the app, you need to either wipe out the created files, or restore them in some fashion. That's fragile and a royal pain. Better to store the files external to the web app.
And (as Bear also alluded to) it's even more of a pain when your web app is an unexploded WAR file. In fact, it's impossible to update that in any way, least of all to add new files to it. Face it, you're going to have to give up on that idea and consider doing it the right way.
Sorry, but it is impossible for that line to yield an NPE. Are you sure that this is the line causing the exception?
But as had already been pointed out: this is a pointless exercise. Just write the file to a temp folder and be done with it.
Joined: Mar 19, 2009
I understand that the right way to do it is to write the file in some temp folder on the server which is outside my application. But will I be allowed to do so on a rented server? If I am correct I will not have the permission to do that. Is that correct?