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 &ge; 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的更多相关文章

  1. 记录 FTPClient 超时处理的相关问题

    apache 有个开源库:commons-net,这个开源库中包括了各种基础的网络工具类,我使用了这个开源库中的 FTP 工具. 但碰到一些问题,并不是说是开源库的 bug,可能锅得算在产品头上吧,各 ...

  2. ftp中ftpClient类的API

    org.apache.commons.NET.ftp  Class FTPClient类FTPClient java.lang.Object java.lang.Object继承 org.apache ...

  3. 记录FTPClient超时处理的相关问题(转)

    https://www.cnblogs.com/dasusu/p/10006899.html 记录 FTPClient 超时处理的相关问题   apache 有个开源库:commons-net,这个开 ...

  4. FTPClient 工具类

    package com.photoann.core.util; import java.io.BufferedInputStream; import java.io.File; import java ...

  5. FTPClient工具类

    package com.vcredit.ddcash.server.commons.net; import com.vcredit.ddcash.server.commons.model.FtpPar ...

  6. [JAVA]Apache FTPClient操作“卡死”问题的分析和解决

    最近在和一个第三方的合作中不得已需要使用FTP文件接口.由于FTP Server由对方提供,而且双方背后各自的网络环境都很不单纯等等原因,造成测试环境无法模拟实际情况.测试环境中程序一切正常,但是在部 ...

  7. ftpclient 遇到的一些问题

    1. FTPFile[] files=ftpClient.listFiles(ftpDirectory); 没有数据 public static boolean ftpLogin(String ser ...

  8. 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 ...

  9. FTPClient类的API

    org.apache.commons.NET.ftp Class FTPClient类FTPClient java.lang.Object java.lang.Object继承 org.apache. ...

  10. Java FtpClient 实现文件上传服务

    一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...

随机推荐

  1. Set与WeakSet

    Set与WeakSet Set对象允许存储任何类型的唯一值,无论是原始值或者是对象引用,Set对象中的值不会重复. WeakSet对象允许存储对象弱引用的唯一值,WeakSet对象中的值同样不会重复, ...

  2. 实例详解在Go中构建流数据pipeline

    本文分享自华为云社区<Go并发范式 流水线和优雅退出 Pipeline 与 Cancellation>,作者:张俭. 介绍 Go 的并发原语可以轻松构建流数据管道,从而高效利用 I/O 和 ...

  3. Swift —— 一、架构解析

    一.简介 OpenStack 对象存储 (swift) 用于冗余.可扩展的数据 使用标准化服务器集群存储PB的存储 可访问的数据.它是一种长期存储系统,可存储大量 可以检索和更新的静态数据.对象存储使 ...

  4. win32-使用EnumWindows比较两个窗口的Z轴

    通过使用EnumWindows()和枚举窗口来手动确定EnumChildWindows()来直接确定哪个窗口在z轴上比另一个窗口高. struct myEnumInfo { HWND hwnd1; H ...

  5. win32 - 监控DNS是否发生改变

    两种方法: 第一种是使用WMI进行后台轮询 第二种是查询注册表对应的DNS键值 Here: HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameter ...

  6. 以二进制文件安装K8S之创建CA根证书

    为etcd和Kubernetes服务启用基于CA认证的安全机制,需要CA证书进行配置. 如果组织能够提供统一的CA认证中心,则直接使用组织颁发的CA证书即可.如果没有统一的CA认证中心,则可以通过颁发 ...

  7. Mqtt开发笔记:Mqtt服务器搭建

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  8. tox包

    官方文档 https://tox.readthedocs.io/en/latest/example/basic.html 官方提供的一个简单的tox.ini/默认环境 [tox] envlist = ...

  9. 【LeetCode剑指offer#05】回文链表的两种解法+删除链表中间节点(链表的基本操作)

    回文链表 给你一个单链表的头节点 head ,请你判断该链表是否为回文链表.如果是,返回 true :否则,返回 false . 示例 1: 输入:head = [1,2,2,1] 输出:true 示 ...

  10. 05、etcd 读请求执行流程

    本篇内容主要来源于自己学习的视频,如有侵权,请联系删除,谢谢. 1.etcd读请求概览 etcd是典型的读多写少存储,在我们实际业务场景中,读一般占据2/3以上的请求.一个读 请求从client通过R ...