I am uploading a file via FTP to a Linux server. I use Apache's FTPClient
.
So let's say I wanted to upload the file C:\\downloads\\13\\myFile.txt
to the server at /data/downloads/13/myFile.txt
Before I upload, I determine whether the directory that it will be sent to exists using listFiles
.
When I say
ftp.listFiles("/data/downloads/13");
I get an array containing one file object, meaning that the path exists (and I don't need to create a folder). However when I say
ftp.listFiles("\\data\\downloads\\13");
I get an empty array, meaning the path does not exist.
The reason for this is because I'm running my application from a Windows machine, so the path separator is different.
The solution I've decided on is to normalize the paths by replacing \
with /
before I proceed with the FTP transactions.
Is this the proper way to address this issue?
Its the proper way to use the ' / ' but dont know the idea why you want to replace the path seperator before the FTP transactions.But you are running from windows you can provide your full path of windows file as a source with the ' / ' and you can use "/data/downloads/13" as your target.
Provide somemore clarification if I misunderstood it.
From RFC 959:
Because there is no pathname standard, an FTP server can choose to use the pathname conventions of their local file system if they wish. You may wish to send a SYST request to the server before you modify your pathname if you intend to connect to any other server.
The SYST command is not required for minimum FTP server implementation, so this command may be unrecognized by some servers. But if the command has been implemented, it will allow you to modify your pathname to be compatible. Here are 5 example responses you may receive:
From the Apache FTPClient documentation you have linked to, the function that sends a SYST request is this I believe, although I am unfamiliar with Java and this Apache client: getSystemType.
And lastly an example.
But yes, if the server only supports a UNIX pathname you are going to have to convert your Windows pathname from '\' to '/'.