How to provide relative path in File class to upload any file? [duplicate]
Asked Answered
P

2

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());
Peritoneum answered 19/5, 2011 at 13:30 Comment(0)
G
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:

Gwenny answered 19/5, 2011 at 13:39 Comment(19)
@BalusC: So i shall replace destinationDir with /files and it should work?Peritoneum
Yes, it will be /files in linux and C:\files in Windows (assuming that server runs on C:).Gwenny
Also do i need getServletContext().getRealPath(/files);? in this situation?Peritoneum
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.Gwenny
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?Peritoneum
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.Gwenny
@BaluC: i'll give it a shot and get back to you. ThanksPeritoneum
I answered a similar question today, you may find it useful as well #6059948Gwenny
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 /.Gwenny
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.Conductivity
I used System.getProperty("user.dir") /files and it works for both windows and linux machines. Thanks allPeritoneum
@Gwenny - 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 ?Gavingavini
@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.Gwenny
Sir will you see #20492520Gavingavini
@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.Gwenny
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 ?Gavingavini
@Java: another related answer with more detail: stackoverflow.com/a/18664715Gwenny
Yes I am asking something like that. Thank you so much.Gavingavini
@Gwenny 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 solutionShaunda
S
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.

Shimberg answered 19/5, 2011 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.