【问题标题】:Upload Download files from FTP site using SFTP使用 SFTP 从 FTP 站点上传下载文件
【发布时间】:2012-06-09 14:09:20
【问题描述】:

我需要知道一种通过 SFTP 连接到 FTP 站点的方法。我正在使用SharpSSH,但找不到执行该程序的示例。

目前,我已经下载了 SharpSSH .DLL 文件并添加为参考。现在我需要编写可以连接的代码,并从 FTP 服务器上传/下载文件。

我该怎么做?帮助。

更新

代码:

//ip of the local machine and the username and password along with the file to be uploaded via SFTP.
 FileUploadUsingSftp("http://Some-sftp-site.com", "username", "password", @"D:\", @"name.txt");

以上代码在Main方法中。

然后;

private static void FileUploadUsingSftp(string FtpAddress, string FtpUserName, string FtpPassword, string FilePath, string FileName)
        {
            Sftp sftp = null;
            try
            {
                // Create instance for Sftp to upload given files using given credentials
                sftp = new Sftp(FtpAddress, FtpUserName, FtpPassword);

                // Connect Sftp
                sftp.Connect();

                // Upload a file
                sftp.Put(FilePath + FileName);

                // Close the Sftp connection
                sftp.Close();
            }
            finally
            {
                if (sftp != null)
                {
                    sftp.Close();
                }
            }
        }

【问题讨论】:

  • 它是 FTP 站点、SFTP 站点还是 FTPS 站点?它们是 3 种不同的东西。
  • 这似乎是 DNS 解析的问题。您的机器能否成功将您的 SFTP 服务器名称解析为 IP 地址?
  • 你是不是把用户名和密码改成FTP的用户名和密码了。
  • 有人能告诉我我应该为string FilePath, string FileName 传递什么吗? FilePath 是在 SFTP 服务器中找到的路径吗?而FileName是要保存在服务器中的文件?
  • 使用这个 - 太棒了! stackoverflow.com/a/5423005/966609

标签: c# ftp sftp sharpssh


【解决方案1】:

你现在做了什么?

我们不能就“如何上传文件”给你一个直接的答案......

这里有一个教程: http://saravanandorai.blogspot.com/2012/01/sftp-and-file-upload-in-sftp-using-c.html

【讨论】:

  • 首先我需要建立到 SFTP 服务器的连接。然后将文件下载到本地目录
  • 看看我提供的教程。它有你想要的一切。
  • 我已经更新了我的问题。根据教程执行此操作时出现异常。你能帮我解决这个问题吗?
  • 有人能告诉我我应该为string FilePath, string FileName传递什么吗? FilePath 是在 SFTP 服务器中找到的路径吗?而FileName是要保存在服务器中的文件?
  • 如果您查看方法,cmets 清楚地显示 FilePath 和 FileName 连接在一起,它们代表您要上传的文件位置的完整路径名。
【解决方案2】:

我认为FtpAddress参数应该没有ftphttp,所以试试下面的:

 FileUploadUsingSftp("Some-sftp-site.com", "username", 
                     "password", @"D:\", @"name.txt");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    相关资源
    最近更新 更多