日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發(fā)布時間:2023/12/15 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux的ftp下载假死,记一次commons-net FTP上传下载卡死 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在利用apache的commons-net包,做FTP上傳下載時,碰到了一個問題:在默認配置下,傳輸大文件會卡死。

commons-net的maven依賴:

commons-net

commons-net

3.6

在翻閱了官方文檔后,發(fā)現(xiàn)了一段描述,原文如下:

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特性。在大文件傳輸時,由于網(wǎng)絡網(wǎng)速比較低,傳輸較慢,在傳輸期間,data connection是在活動的,但是 control connection是空閑的。對于FTP服務器來說,它知道網(wǎng)絡是有活動的。但是對于一些路由器來說,它們將data connection和control connection視為相互關聯(lián)的,發(fā)現(xiàn)control connection空閑就將網(wǎng)絡關閉。

解決方法是發(fā)一個no-op command,類似心跳請求來維持網(wǎng)絡,防止路由器將網(wǎng)絡切斷。但是對于xxFileStream的方法是不適用的,在一開始編碼時正好使用該方法,導致卡死的情況,可以改用retrieveFile和storeFile方法替換。另外根據(jù)業(yè)務情況可以添加一些超時配置:

# 默認超時時間(ms)

defaultTimeout: 3000

# 連接超時時間 (ms)

connectTimeout: 3000

# 數(shù)據(jù)傳輸超時時間(ms)

dataTimeout: 3000

# 每隔xx秒,像FTP服務器發(fā)送一次心跳

controlKeepAliveTimeout: 60

總結(jié):一開始使用commons-net包,很容易碰到的一個坑,此處記一下,希望能幫助到大家,謝謝。

總結(jié)

以上是生活随笔為你收集整理的linux的ftp下载假死,记一次commons-net FTP上传下载卡死的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。