环境: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的更多相关文章

  1. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

    2019独角兽企业重金招聘Python工程师标准>>> 问题 前两天一个学弟在群里面问一个问题: 请问一下用阿里云服务器发送https请求为什么会失败,是需要有些其他什么配置吗? 同 ...

  2. Charles 抓包 Client SSL handshake failed - Remote host closed connection during handshake

    Charles 抓包 https 报错: Client SSL handshake failed - Remote host closed connection during handshake # ...

  3. Bind 远程连接DNS服务器时出现 rndc: connection to remote host closed

    使用命令:rndc -s 192.168.1.2 status 连接远程的bind 搭建的DNS服务器时出现下面的错误:   rndc: connection to remote host close ...

  4. RemoteDisconnected: Remote end closed connection without response

  5. #python# error:http.client.RemoteDisconnected: Remote end closed connection without response

    添加headers user-agent 网络情况不好的状态下也能出现

  6. ssh问题:ssh_exchange_identification: Connection closed by remote host

    ssh问题:ssh_exchange_identification: Connection closed by remote host... 刚刚一个朋友告诉我SSH连接不上服务器了,重启电脑也不管用 ...

  7. TLS是如何保障数据传输安全(中间人攻击)

    前言 前段时间和同事讨论HTTPS的工作原理,当时对这块知识原理掌握还是靠以前看了一些博客介绍,深度不够,正好我这位同事是密码学专业毕业的,结合他密码学角度对tls加解密这阐述,让我对这块原理有了更进 ...

  8. 关于ssh登录出现异常警告:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    提示警告信息如下: arnold@WSN:~$ ssh 10.18.46.111 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  9. Mac和Linux远程连接服务器异常修复(WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!)

    感谢大佬:https://blog.csdn.net/wd2014610/article/details/79945424 一.今天在使用SSH,连接远程服务器的时候,刚开始是没问题的. 后来阿里云主 ...

随机推荐

  1. 学习加密(四)spring boot 使用RSA+AES混合加密,前后端传递参数加解密

      学习加密(四)spring boot 使用RSA+AES混合加密,前后端传递参数加解密 技术标签: RSA  AES  RSA AES  混合加密  整合   前言:   为了提高安全性采用了RS ...

  2. vue中v-model详解

    vue中经常使用到<input>和<textarea>这类表单元素,vue对于这些元素的数据绑定和我们以前经常用的jQuery有些区别.vue使用v-model实现这些标签数据 ...

  3. 51nod 1384:全排列(STL)

    题目链接 记住next_permutation函数的用法,另外string在这里比char[]慢好多啊.. //#include<bits/stdc++.h> //using namesp ...

  4. flask01

    -python中的web框架 -a:socket服务端 b:路由转发 c:模板渲染 -Django:a:用了别人的 b,c自己写的 -Flask:a:用了别人的 b自己写的,c:用了别人的: jinj ...

  5. gitHub那些优秀的库和想要实现的效果

    1. 轮播库SDCycleScrollView 2. 自动布局库SDAutoLayout 3. 类似支付宝福卡滑动切换的效果 https://github.com/huangxuan518/HXCar ...

  6. .NET Core 使用 nlog 进行日志记录

    1.运行环境 开发工具:Visual Studio 2017 JDK版本:.NET Core 2.0 项目管理工具:nuget 2.GITHUB地址 https://github.com/nbfujx ...

  7. Python基础教程(021)--Pycharm简介

    前言 学习Pycharm开发工具 内容 项目:就是一个功能复杂的软件 目标 必须掌握的工具

  8. POJ - 1655 (点分治-树的重心)

    题目:https://vjudge.net/contest/307753#problem/D 题意:给你一棵树,让你求出一个点,让他的最大子树的节点数尽量小 思路:最大子树节点数尽量小,一看就是树的重 ...

  9. Zlib not installed

    若提示:zlib not installed   wget http://zlib.net/zlib-1.2.8.tar.gz   tar zxf zlib-1.2.8.tar.gz cd zlib- ...

  10. js中的attributes和Attribute的用法和区别。

    一:Attribute的几种用法和含义(attributes和Attribute都是用来操作属性的) getAttribute:获取某一个属性的值: setAttribute:建立一个属性,并同时给属 ...