分块上传文件时FTP连接断开的排查求助(FluentFTP/.NET6)
问题排查:FluentFTP上传文件报Connection reset by peer错误
部署于CentOS Stream 8服务器的.NET6 API,使用FluentFTP库向同网段另一台CentOS Stream 8 FTP服务器上传文件时,出现如下报错:
System.AggregateException: Additional error occured while trying to resume uploading the file 'en/image/2714a124-f821-4f8d-9716-faba782566db.JPEG' at position 65536 (Unable to write data to the transport connection: Connection reset by peer.) (You cannot modify the position of a FtpDataStream. This property is updated as data is read or written to the stream.) System.IO.IOException: Unable to write data to the transport connection: Connection reset by peer. System.Net.Sockets.SocketException (104): Connection reset by peer at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.CreateException(SocketError error, Boolean forAsyncThrow) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.SendAsyncForNetworkStream(Socket socket, CancellationToken cancellationToken) at System.Net.Sockets.Socket.SendAsyncForNetworkStream(ReadOnlyMemory`1 buffer, SocketFlags socketFlags, CancellationToken cancellationToken) at System.Net.Sockets.NetworkStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken) at System.Net.Security.SslStream.WriteSingleChunk[TIOAdapter](TIOAdapter writeAdapter, ReadOnlyMemory`1 buffer) at System.Net.Security.SslStream.WriteAsyncInternal[TIOAdapter](TIOAdapter writeAdapter, ReadOnlyMemory`1 buffer) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at System.Net.Security.SslStream.WriteAsyncInternal[TIOAdapter](TIOAdapter writeAdapter, ReadOnlyMemory`1 buffer) ...当前实现逻辑是先将文件写入API服务器本地临时文件再上传,尝试直接通过Stream上传也出现相同异常;FTP服务器上已创建目标文件但无数据写入,而使用FileZilla上传同一文件可正常完成。
排查步骤
1. 对齐传输模式与端口配置
- 显式设置FluentFTP使用被动模式(PASV),和FileZilla默认行为保持一致:
client.EnablePassive = true; - 检查CentOS防火墙配置,确保FTP服务器的控制端口(21)和被动端口范围(如10000-20000)已开放,API服务器能正常访问这些端口。
2. 匹配SSL/TLS加密配置
- 若FTP服务器启用SSL/TLS,确认FluentFTP的加密设置和FileZilla一致:
client.EnableSsl = true; client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13; - 临时关闭FTP服务器的SSL要求,测试是否能正常上传,排除加密层兼容性问题。
3. 调整上传缓冲区与续传策略
- 报错触发在64KB位置,尝试缩小上传缓冲区:
client.SetBufferSize(32768); // 改为32KB - 禁用自动续传功能,避免续传逻辑引发异常:
client.UploadFile(localPath, remotePath, FtpLocalExists.Overwrite, false, FtpVerify.None); // 第四个参数设为false,关闭续传
4. 检查FTP服务器权限与系统限制
- 确认FTP目标目录的读写权限是否分配给FTP用户,临时关闭SELinux测试(
setenforce 0),排除SELinux策略拦截。 - 检查FTP服务器磁盘空间是否充足,以及是否存在文件大小限制(如vsftpd的
max_file_size配置)。
5. 对比FTP命令日志
- 开启FluentFTP的详细日志,记录所有交互命令:
client.LogToConsole = true; // 或写入日志文件 client.LogToFile("/var/log/fluentftp_upload.log"); - 对比FileZilla的调试日志,检查两者的FTP命令序列差异(比如是否缺少
TYPE I二进制模式设置)。
6. 升级库版本或切换上传方式
- 升级FluentFTP到最新稳定版,修复已知的上传bug。
- 尝试同步上传而非异步上传,或使用
UploadFileStream直接上传流,排除异步逻辑问题。
内容的提问来源于stack exchange,提问作者brainpow3r
相关产品推荐
相关产品推荐

