在端口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?
检查服务器回复以查看连接是否真的有效。
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
检查登录是否按预期进行。
使用这些属性MIGHT可防止意外断开连接:超时(以毫秒为单位)。
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();
}
}