簡體   English   中英

使用sftp將文件上傳到服務器主機

[英]Uploading file to Server host using sftp

我目前收到此錯誤:

未處理的異常:System.Net.SocketsExceptions:沒有此類主機是已知的...

不太清楚這意味着什么,但這就是我所做的。

public FileUploader(/*string host, string port, string username, string password,*/ string fileLocation)
    {
      string temp;
      Console.Write("Server URL:");
      this.host = Console.ReadLine();
      Console.Write("Server Port:");
      temp = Console.ReadLine();
      this.port = int.Parse(temp);
      Console.Write("Username:");
      this.username = Console.ReadLine();
      Console.Write("Password:");
      this.password = ReadPassword();
      Console.Write("Folder Location:");
      this.fileLocation = Console.ReadLine();
    }

    public void UploadFile(string pathToFile)
    {
      var client = new SftpClient(host, port, username, password);
      client.Connect();
      var fileStream = new FileStream(pathToFile, FileMode.Open);
      client.BufferSize = 4 * 1024;
      client.UploadFile(fileStream,fileLocation, null);
      client.Disconnect();
      client.Dispose();
    }

當我在主機名中包含“ ftp://”和“ sftp://”時,我遇到了類似的問題。 谷歌在我的搜索中找到了我。 這是因為我從FtpWebRequest切換到SSH.Net ,其中我的主機值以前是URL。

我包含以下代碼作為工作主機名值的示例。 其他人也可以改用IP地址。

const string sFtpHost = "someprefix.somedomain.com";
const int sFtpPort = 22;
const string userName = "SomeUser";
const string password = "SomePassword";

// ...

string localFileName = SomeFileName;
string remoteFileName = @"/SomeSubFolder/" + System.IO.Path.GetFileName(localFileName);

using (var sftp = new SftpClient(sFtpHost, sFtpPort, userName, password))
{
    sftp.Connect();

    using (var file = File.OpenRead(localFileName))
    {
        sftp.UploadFile(input: file, path: remoteFileName, canOverride: true);
    }

    sftp.Disconnect();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM