This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am trying to write an web application which will allow a user to update images being displayed on their web pages. I have written a JSP which allows the user to upload a file of their choice.
Currently all my images are stored in an images folder inside the webapp. I was wanting to upload my images to the same folder, however, when I attempt to create a new file within the JSP it tries to create a file with the following path:
You don't want to use a relative path when working with server side Java. You can never count on knowing what the current working directory is.
The ServletContext.getRealPath method will get you the full file system path of the resource specified . Using getRealPath("/") will get you the full path to the root of your web application.
Note: If your application is ever deployed as a packed war file, getRealPath will return null. This means using it makes your app less portable BUT, if your app needs to be able to write files to the webapp directory structure, it will won't function properly from a packed war file anyway.