How to set file type and file transfer mode in JSch?

Ivan Karotki :

I used Apache Common Net FTPClient and had setup my ftpClient with methods shown below before upload file.

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);

Now I try to use JSch. Is there same setting needed before file uploading and how they looks like? Thanks.

Martin Prikryl :

First of all, it is not clear from your question, whether you are aware that JSch and Apache Commons Net FtpClient each use completely different and unrelated protocols. JSch is an SSH/SFTP client, while FtpClient is an FTP client.


JSch uses an SFTP protocol version 3.

In the SFTP protocol version 3, there are no transfer modes. Or in other words, there is only the binary transfer mode.

And even if JSch used a newer version of SFTP protocol, which supports the ascii mode, the binary mode is the default anyway in SFTP.

So you do not have to do anything.

This is in a contrast to the FTP protocol, which defaults to the ascii mode. So you have explicitly switch to the binary mode with FTP (using the FTPClient.setFileType in case of the Apache Commons Net).


As for the FTPClient.setFileTransferMode – Your FTP code is wrong. The method accepts one of the *_TRANSFER_MODE constants. Never the *_FILE_TYPE. If you use BINARY_FILE_TYPE, it results in sending an invalid MODE I command to the server. Which for sure fails, and consequently has no effect at all. Check the method result code, it for sure returns false.

Remove the call from your FTP code. There is no need to call FTPClient.setFileTransferMode. Apache Commons Net supports only the default "stream" mode anyway (and most FTP servers do not support any other mode either).


Note the confusion in the names. While the ascii/binary are commonly called "transfer modes". In FTP protocol specification (section 3.1.1 of RFC 959), they are actually called "data types" and are governed using TYPE command.

While what FTP specification calls "transmission modes", governed by MODE command (section 3.4), are completely different kind of modes: stream/block/compressed. They are hardly ever set or even mentioned.

As Apache Commons Net FtpClient confusingly names the method for setting "transmission mode" .setFileTransferMode, it frequently leads to the mistakes like you did – People incorrectly use FTPClient.setFileTransferMode to switch between the binary/ascii "data types".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165585&siteId=1