我能够连接到我的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服务器上。
发布于 2014-08-07 09:30:19
我相信您必须在调用Put
时输入完整的文件名。
string strippedFileName = StripPathComponent(FileName);
sftp.Put(FileName,"//u01//" + strippedFileName);
注意,StripPathComponent
没有实现,如果需要的话,您也必须实现它。它将从FileName
中删除路径组件,即删除C:\...\
或..\...\
。
发布于 2016-11-11 16:54:23
我还在搜索如何使用这个库将文件从本地/共享路径上载到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;
}
https://stackoverflow.com/questions/25178410
复制相似问题