使用 Tamir.SharpSSH 上传到 FTP 服务器 C# [英] Upload to FTP server C# using Tamir.SharpSSH
问题描述
我能够连接到我的 sftp 服务器并且我确信这一点,因为我得到了我的服务器的文件列表并且它通过了正确的列表.但是我无法将文件上传到 mysftp 服务器中的文件夹.这是我的代码:
private static void FileUploadUsingSftp(string SFTPAddress, string SFTPUserName, string SFTPPassword,字符串 SFTPFilePath, 字符串文件名){sftp sftp = null;尝试{sftp = new Sftp( SFTPAddress,SFTPUserName, SFTPPassword);//连接 Sftpsftp.Connect();MessageBox.Show("已连接!");//检查我是否确实连接//列出我的sftp服务器文件夹u01中的文件ArrayList 列表;list = sftp.GetFileList("//u01");foreach(列表中的字符串项目){MessageBox.Show(item.ToString());}MessageBox.Show(list.Count.ToString());//上传文件sftp.Put(文件名, "//u01");----->**我在这里遇到异常**MessageBox.Show("已上传!");//关闭Sftp连接sftp.关闭();}捕获(异常前){MessageBox.Show(ex.ToString());}最后{如果(sftp != 空){sftp.关闭();}}}
我收到此异常:
抛出了‘Tamir.SharpSsh.jsch.SftpException’类型的异常."在 Tamir.SharpSsh.jsch.ChannelSftp.put(String src, String dst, SftpProgressMonitormonitor, Int32 mode)在 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:\...\
或 ..\...\
.>
Im able to connect with my sftp server and I'm sure of it because I get the list of files of my server and it passes correct list. But I cant upload file to a folder in mysftp server. Here is my code:
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();
}
}
}
I recieve this exception:
"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)
I've tried using sftp.Put(FileName,SFTPAddress + "//u01");
Ive tried sftp.Put(FileName,SFTPAddress); And it do work but when I look at my sftp server if the file is there, it isn't.
I've tried sftp.Put(FileName,"//u01"); and it throws the same error.
I must upload my file in a folder in my ftp server and one of the folder is u01.
Can anyone help me out. I don't know what's wrong. I'm sure that i'm connected. and when I tried to upload using filezilla it do work so I'm not restricted in writing to our sftp server.
I believe that you have to enter the full filename in your call to Put
.
string strippedFileName = StripPathComponent(FileName);
sftp.Put(FileName,"//u01//" + strippedFileName);
Note that StripPathComponent
is not implemented, you will have to implement that as well if needed. It would strip a path component from FileName
, i.e. remove C:\...\
or ..\...\
.
这篇关于使用 Tamir.SharpSSH 上传到 FTP 服务器 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!