在端口21上建立FTP TLS显式连接后,我想将文件上传到该服务器。致电ftps.storeFile(fileName, inputStream);
之后
我已收到此例外
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
。
该文件本身是在FTP服务器上创建的,但包含0个字节。
我在1.8.172上运行JVM,所以不是Java 7问题。
通过FileZilla连接到FTP服务器,我注意到该服务器使用TLS 1.2,因此已将该协议添加到JVM参数和启用了会话的协议中。结果还是一样。
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
主题已关闭,因为它现在是重复项。这里给出答案: How to connect to FTPS server with data connection using same TLS session?
答案 0 :(得分:0)
检查服务器答复,以查看连接是否确实有效。
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();
}
}