FTPClient TLS 与 FTP 进行数据传输异常:Remote host closed connection during handshake
环境:java JDK 1.8、org.apache.commons-net-3.6.jar、端口已放开
FTPClient ftpClient = new FTPClient(protocol, false);
ftpClient.setRemoteVerificationEnable(false);
ftpClient.setControlKeepAliveTimeout(300);
ftpClient.setDataTimeout(300);
InputStream fin = null;
try {
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (ftpClient.isPositiveComletion(reply)) {
if (ftpClient.login(username, password)) {
ftpClient.feat();
ftpClient.execPBSZ(0);
ftpClient.execPROT("p");
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
try {
fin = new FileInoutStream(new File("C:\\doc_home\\test1.txt"));
} catch (FileNotFoundException e) {
System.out.println("---file not found");
}
String remoteFile = "test1.txt";
ftpClient.mlsd();
if (ftpClient.storeFile(remoteFile, fin)) {
fin.close();
} else {
System.out.println("could not store file");
}
fin.close();
} else {
System.out.println("FTP login failed");
}
} else {
System.out.println("FTP connect to host failed");
}
} catch (IOException ioe) {
ioe.printStackTrace();
System.out.println("FTP client received network error");
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException ioe) {
//do nothing
}
}
}
异常:
javax.net.ssl.SSLHandshakeException:Remote host closed connection during handshake
FTP client received network error
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at org.apache.commons.net.ftp.FTPSClient.openDataConnection(FTPClient.java:646)
at org.apache.commons.net.ftp.FTPSClient.storeFile(FTPClient.java:653)
at org.apache.commons.net.ftp.FTPSClient.storeFile(FTPClient.java:2030)
at ibgdashboardtest.Demo4.main(Demo4.java:75)
Caused by:java.io.EOFException:SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(Unknown Source)
... 9 more
解决方法:自己定义一个类继承FTPSClient,重载_prepareDataSocket_(final Socket socket)方法,添加了TLS的session hash支持并扩展了密钥,使用时用该类来替代FTPSClient的使用
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.Locale;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;
import javax.net.ssl.SSLSocket;
import org.apache.commons.net.ftp.FTPSClient;
public class SSLSessionReuseFTPSClient extends FTPSClient {
// adapted from:
// https://trac.cyberduck.io/browser/trunk/ftp/src/main/java/ch/cyberduck/core/ftp/FTPClient.java
@Override
protected void _prepareDataSocket_(final Socket socket) throws IOException {
if (socket instanceof SSLSocket) {
// Control socket is SSL
final SSLSession session = ((SSLSocket) _socket_).getSession();
if (session.isValid()) {
final SSLSessionContext context = session.getSessionContext();
try {
final Field sessionHostPortCache = context.getClass().getDeclaredField("sessionHostPortCache");
sessionHostPortCache.setAccessible(true);
final Object cache = sessionHostPortCache.get(context);
final Method method = cache.getClass().getDeclaredMethod("put", Object.class, Object.class);
method.setAccessible(true);
method.invoke(cache, String
.format("%s:%s", socket.getInetAddress().getHostName(), String.valueOf(socket.getPort()))
.toLowerCase(Locale.ROOT), session);
method.invoke(cache, String
.format("%s:%s", socket.getInetAddress().getHostAddress(), String.valueOf(socket.getPort()))
.toLowerCase(Locale.ROOT), session);
} catch (NoSuchFieldException e) {
throw new IOException(e);
} catch (Exception e) {
throw new IOException(e);
}
} else {
throw new IOException("Invalid SSL Session");
}
}
}
}
如果出现兼容问题,应用程序可能会通过JDK中将系统设置属性jdk.tls.useExtendedMasterSecret设置为false来禁用此扩展,在JDK 1.8.0_161中设置:
System.setProperty("jdk.tls.useExtendedMasterSecret", "false");
以上解决方法源自:https://stackoverflow.com/questions/32398754/how-to-connect-to-ftps-server-with-data-connection-using-same-tls-session
FTPClient TLS 与 FTP 进行数据传输异常:Remote host closed connection during handshake的更多相关文章
- javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
2019独角兽企业重金招聘Python工程师标准>>> 问题 前两天一个学弟在群里面问一个问题: 请问一下用阿里云服务器发送https请求为什么会失败,是需要有些其他什么配置吗? 同 ...
- Charles 抓包 Client SSL handshake failed - Remote host closed connection during handshake
Charles 抓包 https 报错: Client SSL handshake failed - Remote host closed connection during handshake # ...
- Bind 远程连接DNS服务器时出现 rndc: connection to remote host closed
使用命令:rndc -s 192.168.1.2 status 连接远程的bind 搭建的DNS服务器时出现下面的错误: rndc: connection to remote host close ...
- RemoteDisconnected: Remote end closed connection without response
- #python# error:http.client.RemoteDisconnected: Remote end closed connection without response
添加headers user-agent 网络情况不好的状态下也能出现
- ssh问题:ssh_exchange_identification: Connection closed by remote host
ssh问题:ssh_exchange_identification: Connection closed by remote host... 刚刚一个朋友告诉我SSH连接不上服务器了,重启电脑也不管用 ...
- TLS是如何保障数据传输安全(中间人攻击)
前言 前段时间和同事讨论HTTPS的工作原理,当时对这块知识原理掌握还是靠以前看了一些博客介绍,深度不够,正好我这位同事是密码学专业毕业的,结合他密码学角度对tls加解密这阐述,让我对这块原理有了更进 ...
- 关于ssh登录出现异常警告:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
提示警告信息如下: arnold@WSN:~$ ssh 10.18.46.111 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- Mac和Linux远程连接服务器异常修复(WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!)
感谢大佬:https://blog.csdn.net/wd2014610/article/details/79945424 一.今天在使用SSH,连接远程服务器的时候,刚开始是没问题的. 后来阿里云主 ...
随机推荐
- after()和append()的区别、before()和prepend()区别、appendTo()和prependTo()、insertAfter()和insertBefore()
一.after()和before()方法的区别 after()——其方法是将方法里面的参数添加到jquery对象后面去: 如:A.after(B)的意思是将B放到A后面去: before( ...
- es6 新语法分享给爱前端的伙伴
相信es6大家并不陌生,那么我还是简单介绍一下es6,es是15年发布的,可以用babel转化成es5可以支持低端浏览器,es6是一种新的语法,流行的库基本都是基于es6开发的.所以小伙伴要掌握哦!而 ...
- Git使用包括切换分支
- Python3解leetcode Count Primes
问题描述: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...
- 4412 搭建tftp服务器
搭建服务器 --安装xinetd,sudo apt-get install xinetd --安装tftp和tftpd,sudo apt-get install tftp tftpd --配置/etc ...
- 转载--关于FPGA设计数字信号处理电路的心得
FPGA使用的越来越广泛,除了可用于设计控制电路以为,数字信号处理电路更是FPGA的强项和难点.个人可以说才刚刚入门FPGA设计,也做过一些数字信号处理方面的电路设计,记录下个人心得体会. (一)善用 ...
- VLOOUP
VLOOKUP函数是Excel中的一个纵向查找函数 该函数的语法规则如下:VLOOKUP(lookup_value,table_array,col_index_num,range_lookup) 参数 ...
- 修改element中v-loading的自定义图片
/*elementui loading css 覆盖 开始*/ .el-loading-spinner .circular{ width: 42px; height: 42px; animation: ...
- 【靶场练习_sqli-labs】SQLi-LABS Page-4 (Challenges)
Less-54: ?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where tabl ...
- 使用SSH 工具 Xshell 6连接CentOS 7.4
在使用Xshell 6连接 服务CentOS 7.4之前,首先你要配置好服务器,如果没有配置好服务器的,请访问: (一)小白教小白配置 服务器 好了,开始正文! 首先准备,SSH 工具 Xshell ...