【问题标题】:Unable to upload a file SFTP using SSH.NET in C# - Permission Denied无法在 C# 中使用 SSH.NET 上传文件 SFTP - 权限被拒绝
【发布时间】:2015-12-28 22:52:01
【问题描述】:

我正在尝试使用 C# 使用 SSH.NET 库使用 SFTP 协议上传文件。下面是我正在使用的代码

FileInfo f=new FileInfo("C:\\mdu\\abcd.xml");            
string uploadfile=f.FullName;    
Console.WriteLine(f.Name);
Console.WriteLine("uploadfile"+uploadfile);
var client = new SftpClient(host, port, username, password);
client.Connect();
if(client.IsConnected){
       Console.WriteLine("I AM CONNECTED");
}
var fileStream = new FileStream(uploadfile, FileMode.Open);  
if(fileStream!=null){
            Console.WriteLine("YOU ARE NOT NULL");
}
client.BufferSize = 4 * 1024; 
client.UploadFile(fileStream, f.Name,null);
client.Disconnect();
client.Dispose();

我可以连接,filestream 也不是 NULL。但是我在尝试上传文件时收到PermissionDeniedException

Unhandled Exception: Renci.SshNet.Common.SftpPermissionDeniedException: Permission denied
   at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError)
   at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback)
   at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Boolean canOverride, Action`1 uploadCallback)
   at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Action`1 uploadCallback)
   at movemsi.Program.UploadFile()
   at movemsi.Program.Main(String[] args)

上面的代码中是否缺少任何设置。非常感谢任何帮助。

【问题讨论】:

    标签: c# upload sftp permission-denied ssh.net


    【解决方案1】:

    您需要指定上传文件的完整路径。

    例如:

    client.UploadFile(fileStream, "/home/user/" + f.Name, null);
    

    如果没有路径,SFTP 服务器可能会尝试将文件写入根文件夹或您没有写入权限的其他文件夹(因此 权限被拒绝)。

    【讨论】:

      【解决方案2】:

      你可以这样做:

      FileInfo f = new FileInfo("C:\\mdu\\abcd.xml");            
      string uploadfile = f.FullName;    
      Console.WriteLine(f.Name);
      Console.WriteLine("uploadfile" + uploadfile);
      
      //Passing the sftp host without the "sftp://"
      var client = new SftpClient("ftp.example.com", port, username, password);
      client.Connect();
      if(client.IsConnected)
      {
          var fileStream = new FileStream(uploadfile, FileMode.Open);  
          if(fileStream != null)
          {
              //If you have a folder located at sftp://ftp.example.com/share
              //then you can add this like:
              client.UploadFile(fileStream, "/share/" + f.Name,null);
              client.Disconnect();
              client.Dispose();
          }
      }
      

      【讨论】:

        【解决方案3】:

        我收到此错误是因为我使用 Path.Combine() 作为 UploadFile 参数,如下所示:

        client.UploadFile(fileStream, Path.Combine("/home/user", f.Name), null);
        

        我认为它会使斜线方向错误。所以只需像上面的答案一样进行正常的字符串连接:

        client.UploadFile(fileStream, "/home/user/" + f.Name, null);
        

        【讨论】:

          【解决方案4】:

          我收到此错误是因为我试图上传与现有文件同名的文件。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-10-15
            • 2014-12-06
            • 2015-07-11
            • 1970-01-01
            • 2014-06-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多