使用SSH.NET恢复网络连接后自动恢复SFTP下载

我正在使用SSH.NET库来创建SFTP客户端。 如果网络连接在此超时内再次可用,我需要恢复下载。 我使用下面提到的方法,如许多例子所示。

PrivateKeyFile ObjPrivateKey = new PrivateKeyFile(keyStream); PrivateKeyAuthenticationMethod ObjPrivateKeyAutentication = new PrivateKeyAuthenticationMethod(username, ObjPrivateKey); var connectionInfo = new ConnectionInfo(hostAddress, port, username, ObjPrivateKeyAutentication); try { using (var client = new SftpClient(connectionInfo)) { client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10); client.Connect(); if (!client.IsConnected) { return false; } if (!client.Exists(source)) { return false; } var fileName = Path.GetFileName(source); using (var fs = new FileStream(destination + fileName, FileMode.Create)) { client.DownloadFile(source, fs, printActionDel); fs.Close(); returnState = true; } client.Disconnect(); client.Dispose(); } } 

我正在拔掉网络电缆以中断下载并测试超时情况。 虽然我在超时内再次启用互联网连接以恢复下载,但它没有恢复。 我在这做错了什么? 请指教。

如果您希望SSH.NET在SftpClient.DownloadFile中重新连接,则不会。

您必须自己实施重新连接和传输简历。

 PrivateKeyFile ObjPrivateKey = new PrivateKeyFile(keyStream); PrivateKeyAuthenticationMethod ObjPrivateKeyAutentication = new PrivateKeyAuthenticationMethod(username, ObjPrivateKey); var connectionInfo = new ConnectionInfo(hostAddress, port, username, ObjPrivateKeyAutentication); bool retry = false; do { bool retrying = retry; retry = false; using (var client = new SftpClient(connectionInfo)) { client.Connect(); if (!client.Exists(source)) { return false; } var fileName = Path.GetFileName(source); var destinationFile = Path.Combine(destination, fileName); try { Stream destinationStream; if (retrying) { destinationStream = new FileStream(destinationFile, FileMode.Append); } else { destinationStream = new FileStream(destinationFile, FileMode.Create); } using (destinationStream) using (var sourceStream = client.Open(source, FileMode.Open)) { sourceStream.Seek(destinationStream.Length, SeekOrigin.Begin); // You can simply use sourceStream.CopyTo(destinationStream) here. // But if you need to monitor download progress, // you have to loop yourself. byte[] buffer = new byte[81920]; int read; ulong total = (ulong)destinationStream.Length; while ((read = sourceStream.Read(buffer, 0, buffer.Length)) != 0) { destinationStream.Write(buffer, 0, read); total = total + (ulong)read; // report progress printActionDel(total); } } } catch (SshException e) { retry = true; } } } while (retry); 

或者使用另一个本机支持简历的SFTP库。

例如, WinSCP .NET程序集确实在其Session.GetFiles方法中自动恢复。

 SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = hostAddress, PortNumber = port, UserName = username, SshHostKeyFingerprint = "ssh-dss 2048 0b:77:8b:68:f4:45:b1:3c:87:ad:5c:be:3b:c5:72:78", SshPrivateKeyPath = keyPath }; using (Session session = new Session()) { session.FileTransferProgress += session_FileTransferProgress; session.Open(sessionOptions); var fileName = Path.GetFileName(source); var destinationFile = Path.Combine(destination, fileName); session.GetFiles(source, destinationFile).Check(); } 

WinSCP GUI可以为您生成如上所示的SFTP下载代码模板 。

(我是WinSCP的作者)