在利用apache的commons-net包,做FTP上传下载时,碰到了一个问题:在默认配置下,传输大文件会卡死。

commons-net的maven依赖:

commons-net

commons-net

3.6

在翻阅了官方文档后,发现了一段描述,原文如下:

Control channel keep-alive feature:

Please note: this does not apply to the methods where the user is responsible for writing or reading the data stream, i.e. retrieveFileStream(String) , storeFileStream(String) and the other xxxFileStream methods

During file transfers, the data connection is busy, but the control connection is idle. FTP servers know that the control connection is in use, so won't close it through lack of activity, but it's a lot harder for network routers to know that the control and data connections are associated with each other. Some routers may treat the control connection as idle, and disconnect it if the transfer over the data connection takes longer than the allowable idle time for the router.

One solution to this is to send a safe command (i.e. NOOP) over the control connection to reset the router's idle timer. This is enabled as follows:

ftpClient.setControlKeepAliveTimeout(300); // set timeout to 5 minutes

This will cause the file upload/download methods to send a NOOP approximately every 5 minutes. The following public methods support this:

This feature does not apply to the methods where the user is responsible for writing or reading the data stream, i.e. retrieveFileStream(String) , storeFileStream(String) and the other xxxFileStream methods. In such cases, the user is responsible for keeping the control connection alive if necessary.

The implementation currently uses a CopyStreamListener which is passed to the Util.copyStream(InputStream, OutputStream, int, long, CopyStreamListener, boolean) method, so the timing is partially dependent on how long each block transfer takes.

如上描述,commons-net有一个重要特性,Control channel keep-alive特性。在大文件传输时,由于网络网速比较低,传输较慢,在传输期间,data connection是在活动的,但是 control connection是空闲的。对于FTP服务器来说,它知道网络是有活动的。但是对于一些路由器来说,它们将data connection和control connection视为相互关联的,发现control connection空闲就将网络关闭。

解决方法是发一个no-op command,类似心跳请求来维持网络,防止路由器将网络切断。但是对于xxFileStream的方法是不适用的,在一开始编码时正好使用该方法,导致卡死的情况,可以改用retrieveFile和storeFile方法替换。另外根据业务情况可以添加一些超时配置:

# 默认超时时间(ms)

defaultTimeout: 3000

# 连接超时时间 (ms)

connectTimeout: 3000

# 数据传输超时时间(ms)

dataTimeout: 3000

# 每隔xx秒,像FTP服务器发送一次心跳

controlKeepAliveTimeout: 60

总结:一开始使用commons-net包,很容易碰到的一个坑,此处记一下,希望能帮助到大家,谢谢。

linux的ftp下载假死,记一次commons-net FTP上传下载卡死相关推荐

  1. java ftp ftpclient_详解JAVA中使用FTPClient工具类上传下载

    详解JAVA中使用FTPClient工具类上传下载 在Java程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件.本文简单介绍如何利用jakarta commons中的FTPClie ...

  2. java文件客户端下载_使用Java写一个minio的客户端上传下载文件

    标签:color   ati   tty   java   system   wired   format   media   param 前言: 确保已经安装了minio的服务端 代码: pom.x ...

  3. java jsch下载文件,JSch使用sftp协议实现服务器文件上传下载操作

    Jsch是什么? JSch 是SSH2的一个纯Java实现.它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等.你可以将它的功能集成到你自己的 程序中.同时该项目也提供一个J2 ...

  4. java流式上传下载_精讲RestTemplate第6篇-文件上传下载与大文件流式下载

    C++Templates(第2版英文版) 123.24元 (需用券) 去购买 > 本文是精讲RestTemplate第6篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在S ...

  5. commons fileUpload 文件上传下载

    commons fileUpload 的使用: commons fileUpload 官网:http://commons.apache.org/fileupload/ 在上面可以下载:commons- ...

  6. ftp上传下载文件详解

    首先导入包 import org.apache.commons.NET.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; FTPCli ...

  7. SecureCRT上传下载文件

    2019独角兽企业重金招聘Python工程师标准>>> SecureCRT是一个仿真终端连接工具.它可以方便的连接SSH服务器,远程管理Linux.同时,它还能使用多种协议方便的上传 ...

  8. MobaXterm不能上传下载文件的问题

    前言 前些日子使用MobaXterm来使用Linux服务器的时候碰到了上传,下载文件失败的问题.同时我的Linux服务器使用的是unbuntu系统,这里也有点小坑. 正文 在MobaXterm中上传或 ...

  9. Java实现七牛云上传下载文件或图片

    文章目录 写在前面 一.准备工作 1.1.为什么选择七牛云? 1.2.七牛云注册 二.java操作七牛云对象存储下载 2.1.pom.xml引入依赖 2.2.上传下载具体代码 三.具体业务例子(七牛云 ...

最新文章

  1. ZABBIX安装官方指南
  2. Mysql游标循环遍历_MySQL数据库中,使用游标循环遍历
  3. Ubuntu 11.10 安装Adobe Air 和卸载Air中的软件
  4. 「Apollo」Apollo感知汇总
  5. 【华为云技术分享】#探索鲲鹏#之“在鲲鹏上使用编程语言——C语言
  6. 软件技术基础_前端系列课程(7)-软件技术基础
  7. java十进制转换成二进制数
  8. SES 之全局搜索小记
  9. Java 对象数组的定义与用法详解
  10. mysql iostat_iostat命令详解_MySQL
  11. 数字信号处理——IIR滤波器设计
  12. 如何申请公网IP资源
  13. 计算机上缺少vsix安装程序,VSIX安装程序在Visual Studio 2017年(15.3)
  14. 自定义TimePicker样式,修改时分、分割线分隔冒号的字体、颜色高度等属性
  15. 从达特茅斯会议到图灵奖---人工智能学习分享
  16. Apriori算法的python实现
  17. 排查not eligible for getting processed by all BeanPostProcessors
  18. IDEA常用操作总结(长期更新)
  19. 读书汇(01)--中国人为什么组织不起来
  20. 随机森林python参数_随机森林的参数说明

热门文章

  1. 在EF4.0中获取ObjectContext的数据库连接字符串
  2. asp.net广告控件的使用
  3. Android USB串口开发
  4. Java 接口和抽象类可以被new么?——顺便总结内部类
  5. php远程连接403,php中出现“ HTTP 异常 403 - 禁止访问”解决方法 总结
  6. php 实时监测网站是否异常_网站监控劫持问题,怎么通过网站监控解决劫持问题...
  7. 比较两组数据的差异用什么图更直观_芯片数据分析中常见的一些图的作用
  8. 关于uniapp引入Vant Weapp第三方组件,遇到的问题和解决方式
  9. java循环满足跳出_java跳出循环的几种方式
  10. Range-v3 practical examples