我能够与我的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服务器。
答案 0 :(得分:1)
我认为您必须在致电Put
时输入完整的文件名。
string strippedFileName = StripPathComponent(FileName);
sftp.Put(FileName,"//u01//" + strippedFileName);
请注意,StripPathComponent
未实现,如果需要,您还必须实现它。它会从FileName
中删除路径组件,即删除C:\...\
或..\...\
。
答案 1 :(得分:0)
我还在搜索如何使用此库将文件从本地/共享路径上传到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;
}