Joining remote paths in Java
- by Mickael Marrache
I'm using the FTP library provided by Apache (commons-net). I want to check if a file exists on the FTP server so I use the listFiles method of FTPClient:
ftpClient.listFiles(remoteFileDir + "\\" + fileName);
The current directory is the FTP server root directory. So, the value of remoteFileDir is a path relative to this root directory.
My question concerns the merge between the remote directory path and the file name. What is the right way to do it? For a local file, I would do:
File file = new File(remoteFileDir,fileName);
but here it doesn't work since when I call file.getAbsolutePath(), I get an absolute path for the file in the local current directory which is not what I want. Also, I guess the merging has been done according to my local environment.
PS: I looked at How are paths determined on a remote machines? but it doesn't help me.
Thanks