org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication
Ftp问题
最近遇到了ftp读取中文乱码的问题,代码中使用的是FtpClient。google一下找到了解决方案。
FTP协议里面,规定文件名编码为iso-8859-1,FTP类中默认的编码也是这个。
public FTP() {
this.setDefaultPort(21);
this._replyLines = new ArrayList();
this._newReplyString = false;
this._replyString = null;
this._controlEncoding = "ISO-8859-1";
this._commandSupport_ = new ProtocolCommandSupport(this);
}
参考文章 https://www.cnblogs.com/chenfei0801/p/3427310.html 设置了编码格式
private FTPClient getClient(ConnectReq connectReq) {
String host = StringUtils.substringBefore(connectReq.getConnectUrl(), ":");
int port = Integer.parseInt(StringUtils.substringAfter(connectReq.getConnectUrl(), ":"));
log.info("Connect to Ftp [{}:{}] with user [{}] and passwd [{}].",
host, port, ftpReq.getUserName(), ftpReq.decodePassword());
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(host, port);
log.info("Connected.");
ftpClient.login(ftpReq.getUserName(), ftpReq.decodePassword());
// Use passive mode to pass firewalls.
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
"OPTS UTF8", "ON"))) {
LOCAL_CHARSET = UTF8_CHARSET;
}
ftpClient.setControlEncoding(LOCAL_CHARSET);
ftpClient.enterLocalPassiveMode();
log.info("Logged.");
return ftpClient;
} catch (IOException e) {
log.error("连接ftp服务-{} 出错,原因:{}", ftpReq.getHost(), e);
throw new InnerException(ErrorCode.DSOURCE_ERROR, e);
}
}
发现程序能够连接成功,但是在调用client.logout方法时出现了错误。
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:313)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:479)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:552)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:601)
at org.apache.commons.net.ftp.FTP.quit(FTP.java:809)
at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:979)
...
java.lang.RuntimeException: org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
...
Caused by: org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:313)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:479)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:552)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:601)
at org.apache.commons.net.ftp.FTP.quit(FTP.java:809)
at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:979)
... 28 more
根据FTPClient官方文档的解释
/***
* Logout of the FTP server by sending the QUIT command.
* <p>
* @return True if successfully completed, false if not.
* @exception FTPConnectionClosedException
* If the FTP server prematurely closes the connection as a result
* of the client being idle or some other reason causing the server
* to send FTP reply code 421. This exception may be caught either
* as an IOException or independently as itself.
* @exception IOException If an I/O error occurs while either sending a
* command to the server or receiving a reply from the server.
***/
public boolean logout() throws IOException
{
return FTPReply.isPositiveCompletion(quit());
}
这个错误的出现的可能原因是ftp服务器过早关闭或者其他原因导致服务端返回了421。
继续追踪到后面的异常栈
Connection closed without indication.
这个问题可能是因为FTP服务器服务有故障,或是是网络问题。于是我立马用命令行尝试连接到Ftp上,果然是可以连的,证明ftp服务没问题。那么就是网络问题了。
检查了代码,在代码中使用的是被动模式。FTP服务器一般使用20和21两个端口与客户端进行通信,21端口用来传输FTP的控制命令,20端口用于传输文件数据。
如果是主动模式的话,FTP客户端向服务器的FTP控制端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路;当需要传送数据时,客户端在命令链路上用PORT的命令告诉服务器我开放了某端口,你过来连接我。于是服务器从20端口向客户端的该端口发送连接请求,建立一条数据链路来传送数据。在数据链路建立过程中是服务器主动请求,所以称为主动模式。
subgraph 客户端
client[客户端]
clientPort[打开端口]
client--2.打开-->clientPort
end
subgraph 服务端
server[服务端]
server-->port21
server-->port20
port21(21端口)
port20(20端口)
client--1.连接请求-->port21
client--3.PORT通知-->server
port20--4.建立连接-->clientPort
end
如果是被动模式的话,FTP客户端向服务器的FTP控制端口(默认21)发送连接请求,服务器接受连接,建立一条命令链路;当需要传送数据时,服务器在命令链路上用PASV命令告诉客户端,我打开了某端口,你过来连我。于是客户端向服务器的该端口发送连接请求,建立一条数据链路来传送数据。在数据链路建立的过程中是服务器被动等待客户机的请求,所以称被动模式。
subgraph 客户端
client[客户端]
clientConnect[建立数据链路]
client-->clientConnect
end
subgraph 服务端
server[服务端]
server-->port21
server--2.打开-->serverPort
port21(21端口)
serverPort(打开端口)
client--1.连接请求-->port21
server--3.PASV通知-->client
clientConnect--4.建立连接-->serverPort
end
上文说到代码中用的是被动模式,也就意味着服务端需要打开21端口和另一个端口供客户端连接。我使用的ftp是用docker装的,只透出了21端口,所以使用被动模式连接会出错。于是把这里改成主动模式,连接成功。
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication的更多相关文章
- org.apache.commons.net.ftp
org.apache.commons.NET.ftp Class FTPClient类FTPClient java.lang.Object Java.lang.Object继承 org.apache. ...
- 【FTP】使用org.apache.commons.net.ftp.FTPClient 实现FTP的上传下载
在此之前,在项目中加上FTP的架包 第一步:配置FTP服务器的相关配置 FtpConfig.java 实体类(配置类) package com.sxd.ftp; public class FtpCo ...
- 【FTP】org.apache.commons.net.ftp.FTPClient实现复杂的上传下载,操作目录,处理编码
和上一份简单 上传下载一样 来,任何的方法不懂的,http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net ...
- ftp org.apache.commons.net.ftp.FTPClient 判断文件是否存在
String path = "/SJPT/ONPUT/HMD_TEST/" ; FtpTool.getFTPClient().changeWorkingDirectory(path ...
- Java 利用Apache Commons Net 实现 FTP文件上传下载
package woxingwosu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...
- Java使用Apache Commons Net实现FTP功能
maven依赖: <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> ...
- 使用apache commons net进行ftp传输
apache commons net的maven地址: http://mvnrepository.com/artifact/commons-net/commons-net/3.6 <!-- ht ...
- Apache Camel之FTP组件学习
写在最前面 哎,最近提了离职,手头的活也基本上清理的差不多了.想着这个把月可以舒服的晃悠晃悠的离开,但是运维的小伙伴总是不架势,走之前还是提了个新需求. 先说下需求吧,我们的系统概括的讲就是一个接口系 ...
- Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:
七月 17, 2014 4:56:01 下午 org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service( ...
随机推荐
- Java WebService学习笔记 - Axis进阶(二)
上一篇 Java WebService学习笔记 - Axis(一) 前一篇博文中简单介绍了Axis的使用方法,这篇将介绍一些Axis的一些高级特性 Axis中Handler的使用 Handler ...
- 弹性配置为构建提速 - CODING & 腾讯云 CVM 最佳实践
CODING 中提供了内置云主机用来执行持续集成(CI)中的构建计划,能够胜任大部分构建任务.但如果碰上了大型项目的构建,或者需要在本地服务器生成构建成果,单个计算资源就显得有点捉急了.针对这一部分需 ...
- Python3-gevent模块-单线程下的"并发"-协程
博客转载 http://www.cnblogs.com/alex3714/articles/5248247.html http://www.cnblogs.com/tkqasn/p/5705338.h ...
- Angular 从入坑到挖坑 - 模块简介
一.Overview Angular 入坑记录的笔记第七篇,介绍 Angular 中的模块的相关概念,了解相关的使用场景,以及知晓如何通过特性模块来组织我们的 Angular 应用 对应官方文档地址: ...
- Ubuntu18.04 IP配置问题
18.04 LTS 提供了通过 netplan.io 轻松配置网络连接 参考 Ubuntu18.04 发行release cn.ubuntu.com/server
- 通俗易懂的阿里Sentinel源码分析:如何向控制台发送心跳包?
源码分析 public class Env { public static final Sph sph = new CtSph(); static { // 在Env类的静态代码块中, // 触发了一 ...
- 《JavaScript高级程序设计》(第二版)
这本书的作者是 Nicholas C.Zakas ,博客地址是 http://www.nczonline.net/ ,大家可以去多关注,雅虎的前端工程师,是YUI的代码贡献者,可想而知这本书得含金量, ...
- 二.3.token认证,jwt认证,前端框架
一.token: 铺垫: 之前用的是通过最基本的用户名密码登录我的运维平台http://127.0.0.1:8000/---这种用的是form表单,但是这种对于前后端分离的不适合.前后端分离,应该通过 ...
- 使用Fiddler模拟Post请求
做了一个动态的GIF来做演示,应该更加直观些. (完)
- 关于WebServices的调用
1.使用soapui测试接口是否能通 具体操作步骤请查看:https://www.cnblogs.com/BINDAI/p/13201513.html Soapui请求结果集 2.获得soapu ...