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( ...
随机推荐
- Netty的Marshalling编解码器
1.编码与解码 通常我们习惯将编码(Encode)称为序列化(serialization),它将对象序列化为字节数组,用于网络传输.数据持久化或者其它用途.反之,解码(Decode)称为反序列化 ...
- springBoot--集成RocketMQ
1.导入依赖 <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>sprin ...
- Python实用笔记 (27)面向对象高级编程——使用枚举类
枚举类型定义一个class类型,然后,每个常量都是class的一个唯一实例.Python提供了Enum类来实现这个功能: from enum import Enum Month = Enum('Mon ...
- html+css快速入门教程(1)
1 HTML简介 1.1. 什么是HTML?(了解) HTML是超文本标记语言(HyperText Markup Language,HTML)的缩写.是标准通用标记语言(SGML Standard G ...
- 四. django template模版
往前端浏览器pull一些字符串,这些字符串是一些数据, 那如果想让这些数据按我们的某种格式美化一点/增加样式/图片,就需要用到django提供的模版--模版就是为了让数据看起更美观. 加载模版 dja ...
- typescript 展开操作符,对象属性值更新
- C++ ACE 动态加载链接库
添加头文件 #include <ace/DLL.h> #include <ace/DLL_Manager.h> 定义函数接口 typedef long (*PFN_TEST)( ...
- Centos 6.4 安装Mplayer 播放器
1.Download the rpmforge-release package. URL1:x86_64.rmp URL2:tar.gz 推荐!!! 2.Install DAG's GPG ke ...
- 要想数组用的 6,怎能不懂 java.util.Arrays
java.util.Arrays 类就是为数组而生的专用工具类,基本上常见的对数组的操作,Arrays 类都考虑到了,这让我由衷地觉得,是时候给该类的作者 Josh Bloch.Neal Gafter ...
- html实现时间轴_纯css实现响应式竖着/垂直时间抽布局效果
1.概述 html实现用时间点来展示事件发生点来代替用table展示一条条数据,能够给人清晰.一目了然能够看清事情发生的过程,UI页面也显示的那么清晰.如何用css+html做出时间轴展示事件点的?先 ...