首页 > 解决方案 > System.Net.FtpClient.FtpCommandException:指定的网络名称不再可用

问题描述

下面的代码从 FTP 服务器获取文件并将它们存储在本地。

它适用于 1.8 GB 的文件大小,抛出 System.Net.FtpClient.FtpCommandException: The specified network name is no longer available when it is around 1.99 GB

var destinationPath = Path.Combine(path, ftpListItem.Name);
            ftpClient.ReadTimeout = 45000;
            ftpClient.SocketKeepAlive = true;
            using (var ftpStream = ftpClient.OpenRead(ftpListItem.FullName))
            {
                int bufferToRead = 8;
                int buffSize = (int)ftpStream.Length == 0 ? 1 : (int)ftpStream.Length;
                using (var fileStream = File.Create(destinationPath, buffSize))
                {
                    var buffer = new byte[bufferToRead * 1024 * 1024];
                    int count;
                    while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fileStream.Write(buffer, 0, count);

                    }

                }

            }

在 fileStream.Write(buffer, 0, count); 行抛出异常

例外

System.Net.FtpClient.FtpCommandException: The specified network name is no longer available.
   at System.Net.FtpClient.FtpClient.CloseDataStream(FtpDataStream stream)
   at System.Net.FtpClient.FtpDataStream.Close()
   at System.Net.FtpClient.FtpSocketStream.Dispose()
   at Batch_FtpTransfer.BLL.RequestBLL.downloadFile(RequestDTO request, String path, FtpListItem ftpListItem, FtpClient ftpClient) in C:\SourceCode\Batch_FtpTransfer\Batch_FtpTransfer\Batch_FtpTransfer\BLL\RequestBLL.cs:line 51
   at Program.Main(String[] args) in C:\SourceCode\Program.cs:line 71

标签: ftpftp-client

解决方案


推荐阅读