gpt4 book ai didi

c# - 使用SSH.NET上传到SFTP服务器失败,出现SftpPathNotFoundException: 'The system cannot find the path specified.'

转载 作者:行者123 更新时间:2023-12-02 14:13:41 32 4
gpt4 key购买 nike

我正在尝试使用Renci SSH.NET通过SFTP上传文件。

正在连接:

  • 使用SSH.NET 连接到sFTP站点似乎可行:SftpClientIsConnected返回true。
  • 使用FileZilla 连接会触发以下警告:

    Server's Host Key is Unknown


  • 上传:

    1)使用SftpClientUploadFile方法:
        client.UploadFile(fileStream, "/Path/" + fileName, null);

    ...我得到一个Renci.SshNet.Common.SftpPathNotFoundException:“系统找不到指定的路径。”
  • 我尝试对路径进行硬编码-相同的结果。
  • 我尝试使用SftpClientWorkingDirectory-但这会转换为一串看起来像中文的字符,而且也不起作用。

  • 2)使用SftpClientBeginUploadFile方法:
        client.BeginUploadFile(fileStream, "/Path/" + fileName, asyncCallback, null, UpdateUploadProgress);

    ...我没有任何错误/异常,但是:
  • 文件未上传
  • aSyncCallbackuploadCallback似乎不起作用

  • 3)使用FileZilla,我可以很好地上传(使用与上面完全相同的目标路径:“/ Path / Filename.txt”)

    我的代码:
        var connectionInfo = new ConnectionInfo(IpAddress,
    Port,
    UserName,
    new PasswordAuthenticationMethod(UserName, Password),
    new PrivateKeyAuthenticationMethod("rsa.key"));
    connectionInfo.Encoding = Encoding.Unicode;
    using (var client = new SftpClient(connectionInfo))
    {
    client.Connect();

    if (client.IsConnected)
    {
    Console.WriteLine("SSH-client is connected");
    }
    var fileStream = new FileStream(FileMaker.GetFullyQualifiedPath(), FileMode.Open);
    client.BufferSize = 4 * 1024;
    string fileName = new FileMaker().GetFileName();
    //client.UploadFile(fileStream, "/Path/" + fileName, null);
    AsyncCallback asyncCallback = new AsyncCallback(NotifyUploadComplete);
    client.BeginUploadFile(fileStream, "/Path/" + fileName, asyncCallback, null, UpdateUploadProgress);
    client.Disconnect();
    }

    回调处理程序:
        private void UpdateUploadProgress(ulong uploaded)
    {
    MainViewModel mainViewModel = (MainViewModel)System.Windows.Application.Current.FindResource("mainViewModel");
    mainViewModel.UploadProgress = uploaded;
    }

    private void NotifyUploadComplete(IAsyncResult result)
    {
    MessageBox.Show("File uploaded.", "File uploaded");
    }

    UPDATE :
    我做了一个最小的例子并进行了测试(结果相同)。

    使用UploadFile:
    var connectionInfo = new ConnectionInfo(IpAddress,
    Port,
    UserName,
    new PasswordAuthenticationMethod(UserName, Password),
    new PrivateKeyAuthenticationMethod("rsa.key"));
    connectionInfo.Encoding = Encoding.Unicode;
    using (var client = new SftpClient(connectionInfo))
    {
    client.Connect();

    if (client.IsConnected)
    {
    Console.WriteLine("SSH-client is connected");
    }
    var fileStream = new FileStream(@"C:\File.txt", FileMode.Open);
    client.BufferSize = 4 * 1024;
    client.UploadFile(fileStream, "/SSHUsersPath/File.txt", null);
    //AsyncCallback asyncCallback = new AsyncCallback(NotifyUploadComplete);
    //client.BeginUploadFile(fileStream, "/SSHUsersPath/File.txt", asyncCallback, null, UpdateUploadProgress);
    client.Disconnect();
    }

    这是请求的FileZilla日志文件的摘录:

    2017-09-21 10:08:36 11252 1 Status: Connected to sshserv.CENSORED.com 2017-09-21 10:08:36 11252 1 Status: Retrieving directory listing... 2017-09-21 10:08:36 11252 1 Command: pwd 2017-09-21 10:08:36 11252 1 Response: Current directory is: "/SSHUsersPath" 2017-09-21 10:08:36 11252 1 Command: ls 2017-09-21 10:08:37 11252 1 Status: Listing directory /SSHUsersPath 2017-09-21 10:08:37 11252 1 Status: Directory listing of "/SSHUsersPath" successful 2017-09-21 10:08:58 11252 3 Status: Connecting to sshserv.CENSORED.com... 2017-09-21 10:08:58 11252 3 Response: fzSftp started, protocol_version=8 2017-09-21 10:08:58 11252 3 Command: open "CENSORED@sshserv.CENSORED.com" 22 2017-09-21 10:08:59 11252 3 Command: Trust new Hostkey: Once 2017-09-21 10:09:00 11252 3 Command: Pass: ***** 2017-09-21 10:09:00 11252 3 Status: Connected to sshserv.CENSORED.com 2017-09-21 10:09:00 11252 3 Status: Starting upload of C:\File.txt 2017-09-21 10:09:00 11252 3 Command: cd "/SSHUsersPath" 2017-09-21 10:09:01 11252 3 Response: New directory is: "/SSHUsersPath" 2017-09-21 10:09:01 11252 3 Command: put "C:\File.txt" "File.txt" 2017-09-21 10:09:01 11252 3 Command: local:C:\File.txt => remote:/SSHUsersPath/File.txt 2017-09-21 10:09:01 11252 3 Status: File transfer successful, transferred 4 bytes in 1 second 2017-09-21 10:09:01 11252 3 Status: Retrieving directory listing of "/SSHUsersPath"... 2017-09-21 10:09:01 11252 3 Command: ls 2017-09-21 10:09:01 11252 3 Status: Listing directory /SSHUsersPath 2017-09-21 10:09:02 11252 3 Status: Directory listing of "/SSHUsersPath" successful 2017-09-21 10:09:17 11252 1 Status: Deleting "/SSHUsersPath/File.txt" 2017-09-21 10:09:17 11252 1 Command: rm "/SSHUsersPath/File.txt" 2017-09-21 10:09:18 11252 1 Response: rm /SSHUsersPath/File.txt: OK

    最佳答案

    主要问题是:

    connectionInfo.Encoding = Encoding.Unicode;
    Encoding.Unicode是UTF-16。任何SFTP服务器都不会处理UTF-16编码的文件名。 SFTP服务器应使用UTF-8。因此,SSH.NET默认使用 Encoding.UTF8。如果使用UTF-8,服务器破坏了文件名中的非ASCII字符( .Encoding是关于文件名而不是文件内容的),那一定是因为SFTP已损坏并且使用了一些旧式编码。但可以肯定的是,它不是UTF-16。而是一些 ISO*编码或 Windows-*编码。

    另一个问题是,当您使用 BeginUploadFile时,您不必等待它完成并立即断开连接。

    关于c# - 使用SSH.NET上传到SFTP服务器失败,出现SftpPathNotFoundException: 'The system cannot find the path specified.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46325986/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com