当前位置: 首页 > 知识库问答 >
问题:

FTPSClient正在发送二进制文件:“SSLHandShakeException:握手期间远程主机关闭连接”[重复]

邓季
2023-03-14
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

更新:我已经设法直接从FTP服务器获取日志,其中

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

主题关闭后,它现在是一个重复。这里给出了答案:如何使用相同的TLS会话连接到具有数据连接的FTPS服务器?

共有1个答案

穆建华
2023-03-14

检查服务器回复,看看连接是否真的在工作。

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);

使用此boolean检查登录是否按预期进行。

请同时使用这些属性,以防止意外断开连接:以毫秒为单位的超时。

ftpClient.setDefaultTimeout(timeout);
ftpClient.setDataTimeout(timeout);
ftpClient.setConnectTimeout(timeout);
ftpClient.setControlKeepAliveReplyTimeout(timeout);
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();
        }
    }
 类似资料: