.net - 访问 Windows OpenSSH SFTP 服务器上的文件时出现错误 "Bad message"

标签 .net windows sftp openssh ssh.net

我需要通过 SFTP 从网站传输大文件。我无法将这些文件(即使是暂时的)存储在 Web 服务器上,因此我将文件分块传输到 SFTP 服务器。

我已在另一台 Windows 计算机上安装了 OpenSSH ( version 5_30_2016 ),并尝试使用 SSH.NET 连接到它(2016.0.0 版)。

此示例代码给出了在我的机器上引发异常的原因:

const string PATH = "/D:/sftp/test.xml";
var Client = new SftpClient("host", 22, "username", "password");

Client.Connect();   

if(!Client.Exists(PATH))
{                       
    var s = Client.Create(PATH, 1024 * 4);
    s.Dispose();        
}   

var Data = File.ReadAllBytes(@"D:\Temp\TestFile.xml");  
using(var w = Client.Open(PATH, FileMode.Append, FileAccess.ReadWrite)) // this line throws an exception
{
    w.Write(Data, 0, Data.Length);
}

Client.Disconnect();

每当我尝试连接客户端以追加或打开时,我都会收到 SSH 异常 - 基本上是任何返回 StreamStreamWriter 的操作。

我通过 SFTP 客户端返回的错误是“Bad Message”,这不是很具有描述性。我只是希望我的方法中遗漏了一些明显的东西......!

我也尝试过使用其他 SFTP 库,但它们似乎都依赖于将物理本地文件传递到远程位置,这不是我想要做的。看来 SSH.NET 最适合我的需求,因为它提供了将字节数组和流传递到 SFTP 服务器的方法

堆栈跟踪,带有消息“错误消息”:

   at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError)
   at Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize, Boolean useAsync)
   at Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize)
   at Renci.SshNet.SftpClient.Open(String path, FileMode mode, FileAccess access)

最佳答案

这是FileMode.Append模式。

Windows OpenSSH 服务器似乎不支持。

当您将其替换为 FileMode.Create 时,它就可以工作。


您可以使用 SftpFileStream.Writeoffset 参数来代替 FileMode.Append

关于.net - 访问 Windows OpenSSH SFTP 服务器上的文件时出现错误 "Bad message",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39395340/

相关文章:

c# - VS 团队测试 : Multiple Test Initialize Methods in Test Class

c# - 在 C# 4.0 中在运行时附加属性和方法?

c++ - 在 C++ 中读取 PDB header

.net - "Splicing"代码写入 Windows 可执行文件

linux - tsv 总结 : command not found

python - 使用 Python 直接从 SFTP 服务器上传文件到 Box,无需保存中间文件

ftp - 为什么 SFTP 没有像 FTP 这样的主动/被动模式

javascript - 单击 kogrid 中的复选框时获取行数据

linux scp/sftp命令

c# - 如何在 C# 中从字符串调用委托(delegate)?