首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用C#上传到FTP服务器Tamir.SharpSSH

使用C#上传到FTP服务器Tamir.SharpSSH
EN

Stack Overflow用户
提问于 2014-08-07 09:05:58
回答 2查看 21.9K关注 0票数 1

我能够连接到我的sftp服务器,我确信这一点,因为我得到了我的服务器的文件列表,它会传递正确的列表。但我不能上传文件到一个文件夹在mysftp服务器。这是我的代码:

代码语言:javascript
运行
复制
  private static void FileUploadUsingSftp(string SFTPAddress, string SFTPUserName, string SFTPPassword,
        string SFTPFilePath, string FileName)
    {
        Sftp sftp = null;
        try
        {

            sftp = new Sftp( SFTPAddress,SFTPUserName , SFTPPassword);

            // Connect Sftp
            sftp.Connect();
            MessageBox.Show("Connected!");



            //Check if im surely connected
            //list down files in my sftp server folder u01
            ArrayList list;
            list = sftp.GetFileList("//u01");

            foreach (string item in list)
            {
                MessageBox.Show(item.ToString());
            }
            MessageBox.Show(list.Count.ToString());

            // upload file 
            sftp.Put(FileName, "//u01"); -----> **I get exception here**

            MessageBox.Show("UPLOADED!");


            // Close the Sftp connection
            sftp.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            if (sftp != null)
            {
                sftp.Close();
            }
        }
    }

我收到这个例外:

代码语言:javascript
运行
复制
"Exception of type 'Tamir.SharpSsh.jsch.SftpException' was thrown."
at Tamir.SharpSsh.jsch.ChannelSftp.put(String src, String dst, SftpProgressMonitormonitor, Int32 mode)
at Tamir.SharpSsh.Sftp.Put(String fromFilePath, String toFilePath)

我试过使用sftp.Put(FileName,SFTPAddress + "//u01");

我尝试过sftp.Put(FileName,SFTPAddress);它确实有效,但是当我查看我的sftp服务器(如果文件在那里)时,它没有工作。

我已经尝试过sftp.Put(FileName,"//u01");它也抛出了相同的错误。

我必须将文件上传到ftp服务器中的一个文件夹中,其中一个文件夹是u01

有人能帮帮我吗。我不知道怎么回事。我肯定我是有联系的。而且,当我试图使用filezilla上传时,它可以工作,所以我不会被限制在sftp服务器上。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-07 09:30:19

我相信您必须在调用Put时输入完整的文件名。

代码语言:javascript
运行
复制
string strippedFileName = StripPathComponent(FileName);
sftp.Put(FileName,"//u01//" + strippedFileName);

注意,StripPathComponent没有实现,如果需要的话,您也必须实现它。它将从FileName中删除路径组件,即删除C:\...\..\...\

票数 1
EN

Stack Overflow用户

发布于 2016-11-11 16:54:23

我还在搜索如何使用这个库将文件从本地/共享路径上载到SFTP服务器,并最终找到了解决方案。您可以使用下面的代码。

代码语言:javascript
运行
复制
string host="ssgty";
string username="usr";
string password="passw";
int port=22;
string fromFile=@"D:\Test.txt";
string toFile=@"/dmon/myfolder/Test.txt";

public string CopyToFTP(string host, string username, string password, int port, string fromFile, string toFile)
{
      string error = "";
      try
      {
           Scp scp = new Scp(host, username, password);
           scp.Connect(port);
           scp.To(fromFile, toFile);
      }
      catch (Exception ex)
      {
           error = ex.Message;
      }
      return error;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25178410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档