manash
manash

Reputation: 7106

Joining remote paths in Java

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

Upvotes: 3

Views: 671

Answers (1)

manash
manash

Reputation: 7106

I found the method changeWorkingDirectory of FTPClient.

ftpClient.changeWorkingDirectory(remoteFileDir);
FTPFile[] names = ftpClient.listFiles(fileName);

In this case, I don't have to specify a delimiter.

Upvotes: 1

Related Questions