FtpClient一定要setSotimeOut、setDataTimeout
SotimeOut,简单说就是读取数据时阻塞链路的超时时间。
/**
* Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}
* with the specified timeout, in milliseconds. With this option set
* to a non-zero timeout, a read() call on the InputStream associated with
* this Socket will block for only this amount of time. If the timeout
* expires, a <B>java.net.SocketTimeoutException</B> is raised, though the
* Socket is still valid. The option <B>must</B> be enabled
* prior to entering the blocking operation to have effect. The
* timeout must be {@code > 0}.
* A timeout of zero is interpreted as an infinite timeout.
* (设置一个超时时间,用来当这个 Socket 调用了 read() 从 InputStream 输入流中
* 读取数据的过程中,如果线程进入了阻塞状态,那么这次阻塞的过程耗费的时间如果
* 超过了设置的超时时间,就会抛出一个 SocketTimeoutException 异常,但只是将
* 线程从读数据这个过程中断掉,并不影响 Socket 的后续使用。
* 如果超时时间为0,表示无限长。)
* (注意,并不是读取输入流的整个过程的超时时间,而仅仅是每一次进入阻塞等待输入流中
* 有数据可读的超时时间)
* @param timeout the specified timeout, in milliseconds.
* @exception SocketException if there is an error
* in the underlying protocol, such as a TCP error.
* @since JDK 1.1
* @see #getSoTimeout()
*/
public synchronized void setSoTimeout(int timeout) throws SocketException {
//.....
}
/**
* Sets the timeout in milliseconds to use when reading from the
* data connection. This timeout will be set immediately after
* opening the data connection, provided that the value is ≥ 0.
* <p>
* <b>Note:</b> the timeout will also be applied when calling accept()
* whilst establishing an active local data connection.
* @param timeout The default timeout in milliseconds that is used when
* opening a data connection socket. The value 0 means an infinite timeout.
*/
public void setDataTimeout(int timeout)
{
__dataTimeout = timeout;
}
如果不设置,可能会在网络波动时阻塞,至此无限时阻塞在此!!!
所以使用ftp时,一般需要设置timeout来保证程序正常运行(不卡死)。
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
ftpClient.setDefaultTimeout(10000);
ftpClient.setConnectTimeout(10000);
ftpClient.setDataTimeout(10000);
// 连接FTP服务器
ftpClient.connect(host, port);
// 登陆FTP服务器
ftpClient.login(userName, password);
// 中文支持
ftpClient.setControlEncoding("UTF-8");
// 设置文件类型为二进制(如果从FTP下载或上传的文件是压缩文件的时候,不进行该设置可能会导致获取的压缩文件解压失败)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 主动模式
//ftpClient.enterLocalActiveMode();
// 被动模式 每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据 防止假卡死
ftpClient.enterLocalPassiveMode();
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
logger.error("连接FTP失败");
ftpClient.disconnect();
} else {
ftpClient.setSoTimeout(10000);
logger.info("FTP连接成功!");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("登陆FTP失败,请检查FTP相关配置信息是否正确!", e);
}
return ftpClient;
FtpClient一定要setSotimeOut、setDataTimeout的更多相关文章
- 记录 FTPClient 超时处理的相关问题
apache 有个开源库:commons-net,这个开源库中包括了各种基础的网络工具类,我使用了这个开源库中的 FTP 工具. 但碰到一些问题,并不是说是开源库的 bug,可能锅得算在产品头上吧,各 ...
- ftp中ftpClient类的API
org.apache.commons.NET.ftp Class FTPClient类FTPClient java.lang.Object java.lang.Object继承 org.apache ...
- 记录FTPClient超时处理的相关问题(转)
https://www.cnblogs.com/dasusu/p/10006899.html 记录 FTPClient 超时处理的相关问题 apache 有个开源库:commons-net,这个开 ...
- FTPClient 工具类
package com.photoann.core.util; import java.io.BufferedInputStream; import java.io.File; import java ...
- FTPClient工具类
package com.vcredit.ddcash.server.commons.net; import com.vcredit.ddcash.server.commons.model.FtpPar ...
- [JAVA]Apache FTPClient操作“卡死”问题的分析和解决
最近在和一个第三方的合作中不得已需要使用FTP文件接口.由于FTP Server由对方提供,而且双方背后各自的网络环境都很不单纯等等原因,造成测试环境无法模拟实际情况.测试环境中程序一切正常,但是在部 ...
- ftpclient 遇到的一些问题
1. FTPFile[] files=ftpClient.listFiles(ftpDirectory); 没有数据 public static boolean ftpLogin(String ser ...
- 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, f ...
- FTPClient类的API
org.apache.commons.NET.ftp Class FTPClient类FTPClient java.lang.Object java.lang.Object继承 org.apache. ...
- Java FtpClient 实现文件上传服务
一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...
随机推荐
- java 从零开始手写 RPC (06) reflect 反射实现通用调用之客户端
通用调用 java 从零开始手写 RPC (01) 基于 socket 实现 java 从零开始手写 RPC (02)-netty4 实现客户端和服务端 java 从零开始手写 RPC (03) 如何 ...
- Swoole从入门到入土(3)——TCP服务器[基本配置项]
在这一节的开篇,让我们先解决上一节的"配置"话题.对于server对象,有很多配置项决定了服务端的行为,可以用set的函数进行配置的设置. 1.函数set:用于设置运行时的各项参数 ...
- Laravel入坑指南(10)——事件Event
不知不觉,我们已经来到了第10小节.这一小节,我们一起讨论关于"事件"这个话题.众所周知,从二进制到汇编,再到高等级语言,这一路发展下来,代码都是顺序执行的,那么事件是什么?这个事 ...
- Error: testWhileIdle is true, validationQuery not set
说明 使用springboot连接数据库,启动的时候报错:testWhileIdle is true, validationQuery not set.但是不影响系统使用,数据库等一切访问正常.记录备 ...
- spring boot实现邮箱验证码注册
最近在设计自己的博客系统,涉及到用户注册与登录验证. 注册这地方我先采用最传统的邮箱验证码方式.具体的实现方式如下: 1.有关如何配置spring boot发送邮件,请参考我的另一篇文章: https ...
- SpringBoot整合Swagger2实现接口文档
展示一下 访问方式一 访问地址:http://localhost:8080/swagger-ui.html#/ 首页 详情页 访问方式二 访问地址:http://localhost:8080/doc. ...
- dpt-shell 抽取壳实现原理分析(执行逻辑)
开源项目位置(为大佬开源精神点赞) https://github.com/luoyesiqiu/dpt-shell 抽取壳分为两个步骤 加壳逻辑: 一 对apk进行解析,将codeItem抽出到一个文 ...
- requests请求超时尝试重连的3种方式
参考文档 https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#module-urllib3.util.retry ...
- 用Docker发布Study.BlazorOne.Blazor到公网测试服务器
# 1.准备公网上的测试数据库. 之前我们在Visual Studio里面调试的时候,使用的都是localhost的数据库.现在需要在公网上准备一个SQL Server.然后执行下面的步骤 1)把St ...
- 【Azure Cloud Service(Extended Support)】如何使用外延服务迁移应用?
问题一:迁移到云服务扩展后,之前经典版的云服务的部署槽会变成单一的部署槽,关于两个云服务扩展版之间的部署交换能否提供一个演示? 对于具有双槽的云服务(Classic),根据文档中的建议,在迁移到云服务 ...