I am uploading a file, for which I want to provide a relative path, because the program should work in both linux and windows env.
This is what I am using to upload
realPath = getServletContext().getRealPath(/files);
destinationDir = new File(realPath);
if(!item.isFormField())
{
File file = new File(destinationDir,item.getName());
item.write(file);
}
Any other means to directly provide relative path here
File file = new File(destinationDir,item.getName());
Never do that in a webapp. The working directory is not controllable from inside the code.
Just use
"/path/to/uploaded/files". It works equally in both environments. In Windows it will only be the same disk as where the server runs.You should not store the files in the webcontent. This will fail when the WAR is not expanded and even when it is, all files will get lost whenever you redeploy the WAR. Store them outside the WAR in an absolute location, e.g.
/path/to/uploaded/filesas suggested before.See also: