0

I'm using the following code:

val ftpClient = FTPSClient()
ftpClient.connect(host, port)

And getting the following error:

org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-BIS FileExchange Gateway 2.70.1

It's as if the client is not even speaking SSL when it connects but I'm stumped.

Attempt to "coerce" SSL sockets produce no difference:

val sslContext = SSLContext.getInstance("SSL").apply {
    init(null, arrayOf<TrustManager>(TrustManagerUtils.getAcceptAllTrustManager()), null)
}
val ftpClient = FTPSClient(sslContext)
ftpClient.connect(host, port)

Setting the socket factory produced a socket closed exception:

ftpClient.setSocketFactory(sslContext.socketFactory)

I should note that I can operate the endpoint using sftp so I can infer that the endpoint works correctly.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
F. P. Freely
  • 1,026
  • 14
  • 24

2 Answers2

1

FTPSClient implements FTPS, i.e. FTP over SSL/TLS. It is expected that you connect to an FTP server with TLS support. The error message indicates though that you connect to a SSH server, not a FTP server. This means that you are probably trying to use SFTP (file transfer over SSH) not FTPS. Thus, there is a mismatch in the protocol spoken between client and server.

For how to use SFTP instead of FTPS see for example How to retrieve a file from a server via SFTP?.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
0

This finally worked for me and some of it may have to do with the way the endpoint is configured but I'm not sure.

ftpClient.connect(host)
// This is very important!
ftpClient.execPROT("P")
ftpClient.login(username, password)
// This is very important!
ftpClient.enterLocalPassiveMode()
F. P. Freely
  • 1,026
  • 14
  • 24
  • 1
    The key difference seems to be that now you are connecting to the default FTP port (21) via `ftpClient.connect(host)`. While before you were probably connecting to SSH/SFTP port 22 via `ftpClient.connect(host, port)` (with `port=22`). – Martin Prikryl Apr 22 '22 at 20:05