Node.js使用basic-ftp连接Pure-FTPd下载文件时遇FIN包异常错误
Node.js中basic-ftp连接Pure-FTPd下载文件报错:Server sent FIN packet unexpectedly
错误信息
Error: Server sent FIN packet unexpectedly, closing connection. at Socket.<anonymous> (/home/schemannkurier/api/sk_backend/node_modules/basic-ftp/dist/FtpContext.js:129:56) at Socket.emit (node:events:526:35) at Socket.emit (node:domain:488:12) at endReadableNT (node:internal/streams/readable:1589:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
FTP会话日志
Connected to 192.168.179.71:21 (No encryption) < 220---------- Welcome to Pure-FTPd [TLS] ---------- ... (server welcome message) > OPTS UTF8 ON < 200 OK, UTF-8 enabled ... (login and feature negotiation) > PASV < 227 Entering Passive Mode (192,168,179,71,182,86) > RETR /schemannkurier/Gutschein_Example.odt < 150-Accepted data connection 150 63.4 kbytes to download Downloading from 192.168.179.71:46678 (No encryption) > QUIT Error: Server sent FIN packet unexpectedly, closing connection. ... (more details in the error message)
代码示例
const ftp = require("basic-ftp"); const FTPclient = new ftp.Client(); async function connectFTP() { try { FTPclient.ftp.verbose = true; await FTPclient.access({ host: "192.168.179.71", user: "admin", password: "******", // (masked for security) port: 21, secure: false }); console.log(await FTPclient.list()); await FTPclient.downloadTo("/home/schemannkurier/Documents/skbackend/otheruploads/Gutschein_Example.odt", "/schemannkurier/Gutschein_Example.odt"); } catch (err) { console.log(err); } } connectFTP();
额外细节
- Node.js版本:20.10.0
- FTP服务器:Pure-FTPd [TLS]
- 使用basic-ftp库进行FTP交互
解决思路与建议
- 检查被动模式端口与防火墙:Pure-FTPd的被动模式依赖特定端口范围,先确认服务器配置的
PassivePortRange,确保该范围的端口在服务器防火墙和客户端网络防火墙中都已开放。日志中使用的被动端口是46678,对应服务器配置的端口段需包含此端口。 - 调整连接超时设置:初始化Client时设置更长的超时时间,比如
new ftp.Client(30000)(30秒),避免因文件下载耗时超过默认超时导致连接被强制关闭。 - 切换主动模式尝试:在
access配置中添加pasv: false切换为主动模式(PORT),但需确保客户端的20端口未被防火墙拦截。 - 检查Pure-FTPd配置限制:查看服务器是否设置了下载速率限制(
LimitRecvRate)、连接空闲超时(MaxIdleTime)或用户下载权限限制,这些都可能导致连接提前关闭。 - 更新basic-ftp库:执行
npm update basic-ftp升级到最新版本,可能修复库中存在的连接处理bug。 - 验证文件与权限:用其他FTP客户端(如FileZilla)尝试下载目标文件,确认文件本身完整且权限正常,排除服务器端文件问题。
内容的提问来源于stack exchange,提问作者ennolichmannse
相关产品推荐
相关产品推荐

