Tim*_*jan 22 c# upload sftp permission-denied ssh.net
我正在尝试使用SF.NET协议使用C#使用SSH.NET库上传文件.以下是我正在使用的代码
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();
Run Code Online (Sandbox Code Playgroud)
我能够连接,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)
Run Code Online (Sandbox Code Playgroud)
我上面的代码中是否有任何设置.任何帮助深表感谢.
Mar*_*ryl 33
您还需要指定上传文件的完整路径.
例如:
client.UploadFile(fileStream, "/home/user/" + f.Name, null);
Run Code Online (Sandbox Code Playgroud)
如果没有该路径,SFTP服务器可能会尝试将该文件写入根文件夹或您没有写入权限的其他文件夹(因此权限被拒绝).
你可以这样做:
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();
}
}
Run Code Online (Sandbox Code Playgroud)