Different path separators during FTP transaction b

2019-08-31 05:08发布

问题:

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?

回答1:

From RFC 959:

pathname

     Pathname is defined to be the character string which must be
     input to a file system by a user in order to identify a file.
     Pathname normally contains device and/or directory names, and
     file name specification.  FTP does not yet specify a standard
     pathname convention.  Each user must follow the file naming
     conventions of the file systems involved in the transfer.


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.

4.1.3.  FTP SERVICE COMMANDS
     ...
     SYSTEM (SYST)

            This command is used to find out the type of operating
            system at the server.  The reply shall have as its first
            word one of the system names listed in the current version
            of the Assigned Numbers document [4].


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:

 215 UNIX Type: L8
 215 UNIX Type: L8 Version: BSD-44
 215 NetWare system type.
 215 MACOS Peter's Server
 215 AmigaOS


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 '/'.



回答2:

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.



标签: java ftp