5

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());
abarisone
  • 3,707
  • 11
  • 35
  • 54
AabinGunz
  • 12,109
  • 54
  • 146
  • 218

2 Answers2

9

I want to provide a relative path

Never do that in a webapp. The working directory is not controllable from inside the code.


because the program should work in both linux and windows env.

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.

File file = new File("/path/to/uploaded/files", filename);
// ...

This is what i am using to upload

 realPath = getServletContext().getRealPath(/files);
 destinationDir = new File(realPath);

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/files as suggested before.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC: So i shall replace `destinationDir` with `/files` and it should work? – AabinGunz May 19 '11 at 13:41
  • Yes, it will be `/files` in linux and `C:\files` in Windows (assuming that server runs on C:). – BalusC May 19 '11 at 13:42
  • Also do i need `getServletContext().getRealPath(/files);`? in this situation? – AabinGunz May 19 '11 at 13:42
  • No, definitely not. The files will get lost whenever you redeploy the WAR. Use a fixed path. See also the "See also" links in my answer. – BalusC May 19 '11 at 13:43
  • I think you misinterpreted, I am not checking for windows or linux box, if I simply replace `destinationDir` with `/files` will it work for both the platforms? – AabinGunz May 19 '11 at 13:44
  • I think you misinterpreted. Do not use `getRealPath()`. Just use `new File("/files", filename)`. It will in linux end up in `/files` and in Windows in `C:\files`. – BalusC May 19 '11 at 13:44
  • @BaluC: i'll give it a shot and get back to you. Thanks – AabinGunz May 19 '11 at 15:27
  • I answered a similar question today, you may find it useful as well http://stackoverflow.com/questions/6059448/using-getsystemresource-to-access-files-from-a-servlet – BalusC May 19 '11 at 15:29
  • By the way, after a second read of the question/comments, I think you misinterpreted the meaning of "relative path". Relative paths are paths which **do not** start with `/`. So you should never use `new File("path", "filename");`. Always use absolute paths, i.e. starting with `/`. – BalusC May 19 '11 at 15:34
  • I agree, you shouldn't use relative path. I disagree, you shouldn't use path starting with ``/`` on Windows. My arguments are the exact same for both statements: you can't control the partition the data will be stored to. Export the path to a configuration file if you need multi-plateform support. – Vivien Barousse May 19 '11 at 16:27
  • I used `System.getProperty("user.dir")` `/files` and it works for both windows and linux machines. Thanks all – AabinGunz May 20 '11 at 07:51
  • @BalusC - Then where this /path/to/uploaded/files directory reside ? I have got also same problem. right now I am uploading data in app but I am getting problem due to it. So how to create that path and where it will be created ? – Java Curious ღ Dec 10 '13 at 10:56
  • @Java: in Windows, open the file explorer, click "New Folder" button to create a new folder at the desired location `/path/to/uploaded/files` (whatever path you want). Then tell the app to use that folder. – BalusC Dec 10 '13 at 11:17
  • Sir will you see http://stackoverflow.com/questions/20492020/how-to-upload-image-at-specific-place-or-path/20492215?noredirect=1 – Java Curious ღ Dec 10 '13 at 11:20
  • @Java: I can't imagine how it's hard to create folders on the disk file system. Every kid learns that at the primary school. If your actual problem is that you can't do that because you've somehow chosen for a 3rd party host instead of a fully managed host and thus you don't have full access to the server's disk file system, then you should say that so instead of keeping asking how to create folders on the disk file system. Then one can propose an alternative, e.g. ask the 3rd party host's support team where you can write files, or use instead a SQL database to store the files. – BalusC Dec 10 '13 at 11:26
  • I also know how to create folder on disk but due to some problem I am getting problem. I have tried `ServletContext sc=request.getSession().getServletContext();` and `sc.getRealPath("product_images\\")` and it works, I am beginner so I am confused that's why I am asking otherwise I also don't like to waste my time in all this thing, I a trying to learn but if someone support me then I can learn faster. I just want ask you that suppose I ll create directory like `File dir = new File("tmp/test"); dir.mkdirs();`, then where will it create it ? in app or anywhere else ? – Java Curious ღ Dec 10 '13 at 11:32
  • @Java: another related answer with more detail: http://stackoverflow.com/a/18664715/ – BalusC Dec 10 '13 at 11:36
  • Yes I am asking something like that. Thank you so much. – Java Curious ღ Dec 10 '13 at 11:45
  • @BalusC i got stuck on that, i used this 'String submissionFileName = uploadRequest.getFileName("file");' ' File submissionFile = uploadRequest.getFile("file"); ' ' File destination = null;' and the file is uploaded to temp folder, please give me if you have any solution – Akash May 29 '15 at 12:32
0

As BalusC points out, you do not want to save files to a destination found with getRealPath. Not only will such files be wiped out when you redeploy, but they will also be available for download by any user who can reach your web site, which, depending on what you're up to, may be a serious security problem.

When I'm faced with this problem, I normally create a properties file, and put the directory in which to save the files in the properties file. Then I can have a different path for Linux and Windows, or for server A and server B. It may work for you to just invent a path and hardcode it, but I often find that I need different paths on different servers. Like, in my present project we have three instances of Tomcat running on the same physical box for different stages of testing. We don't want files they write to all go to the same directory: each should have its own.

Jay
  • 26,876
  • 10
  • 61
  • 112