0

After establishing FTP TLS explicit connection on port 21 I've want to upload a file to that server. After calling ftps.storeFile(fileName, inputStream); I've received this exception javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake. The file itself gets created on the FTP server but it's empty containign 0 bytes. I'm running my JVM on 1.8.172 so it's not the case of Java 7 problems.

By connecting to the FTP server via FileZilla I've noticed that this server uses TLS 1.2 so I've added that protocol to the JVM arguments AND session enabled protocols. Still the same result.

FTPSClient ftps = new FTPSClient(false);
ftps.connect(host, port) //The port is 21
ftps.login(user, password);
ftps.execPROT("P"); //Turning file transfer encryption which is required by the server
ftps.setEnabledProtocols(new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"});
ftps.changeWorkingDirectory(ftpCatalog); //Catalog gets changed succesfully
ftps.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftps.setFileType(FTP.BINARY_FILE_TYPE);
ftps.setCharset(StandardCharsets.UTF_8);
FileInputStream inputStream = new FileInputStream(filePath);
ftps.storeFile(fileName, inputStream)// <--- Here the exception's gets thrown

UPDATE: I've managed to get the logs directly from the FTP server in which was

450 TLS session of data connection has not resumed or the session does not match the control connection

Topic closed since, it's now a duplicate. Answer is given here: How to connect to FTPS server with data connection using same TLS session?

1 Answers1

0

Check the server reply to see if the connection is really working.

private static void showServerReply(FTPClient ftpClient) {
        String[] replies = ftpClient.getReplyStrings();
        if (replies != null && replies.length > 0) {
            for (String aReply : replies) {
                System.out.println("SERVER: " + aReply);
            }
        }
    }


boolean success = ftpClient.login(user, pass);

Use this boolean to check if the login went as expected.

Use these properties aswell that MIGHT prevent unexpected disconnections: timeout in miliseconds.

ftpClient.setDefaultTimeout(timeout);
ftpClient.setDataTimeout(timeout);
ftpClient.setConnectTimeout(timeout);
ftpClient.setControlKeepAliveReplyTimeout(timeout);

On the single file sending use something like this:

public boolean uploadSingleFile(FTPClient ftpClient, String localFilePath, String remoteFilePath)
            throws IOException {
        File localFile = new File(localFilePath);

        InputStream inputStream = new FileInputStream(localFile);
        try {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            return ftpClient.storeFile(remoteFilePath, inputStream);
        } finally {
            inputStream.close();
        }
    }
Matheus Ribeiro
  • 143
  • 1
  • 15
  • I've already checked and I get a "230 Logged on" response after login method is called. Settings the timeouts did not helped, the exception is thrown immediately and the source file is only 1KB. Using your method of sending file which bassicly meant not setting the FileTransferMode, charset, localPassiveMode etc. didin't helped either. I still get the same result – Karol Kreczman Feb 15 '19 at 14:32
  • did you create this ftp server? or you are acessing an external one? – Matheus Ribeiro Feb 15 '19 at 14:34
  • It's an external one provided by the client – Karol Kreczman Feb 15 '19 at 14:34
  • So it's probably something on the server, not the access methods you are using. – Matheus Ribeiro Feb 15 '19 at 14:35
  • I've done it using FileZilla and the file get's transfered without a problem. Same host, same login, same password, same port. Maybe there's a problem with the certificate I have in my app ? – Karol Kreczman Feb 15 '19 at 14:41