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

我能够连接我的sftp服务器,我很确定它,因为我得到了我的服务器的文件列表,它通过了正确的列表。 但是我无法将文件上传到mysftp服务器中的文件夹。 这是我的代码:

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(); } } } 

我接受了这个例外:

 "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服务器。

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

 string strippedFileName = StripPathComponent(FileName); sftp.Put(FileName,"//u01//" + strippedFileName); 

请注意,未实现StripPathComponent ,如果需要,您还必须实现它。 它将从FileName剥离路径组件,即删除C:\...\..\...\

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

 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; }