用C#连接SFTP服务器并进行上传下载文件

1、使用软件连接可采用WinSCP进行:

文件协议选择SFTP,端口号默认22

用C#连接SFTP服务器并进行上传下载文件

2、使用C#代码操作

参考:http://www.cnblogs.com/binw/p/4065642.html

主要引用第三方库Tamir.SharpSSH.dll,同时还要引用DiffieHellman.dll、Org.Mentalis.Security.dll

clsSFTPHelper.cs

 using System;
using Tamir.SharpSsh.jsch;
using System.Collections; public class clsSFTPHelper
{
private Session m_session;
private Channel m_channel;
private ChannelSftp m_sftp; //host:sftp地址 user:用户名 pwd:密码
public clsSFTPHelper(string server, string user, string pwd, int port)
{
JSch jsch = new JSch();
m_session = jsch.getSession(user, server, port);
MyUserInfo ui = new MyUserInfo();
ui.setPassword(pwd);
m_session.setUserInfo(ui);
} //SFTP连接状态
public bool Connected { get { return m_session.isConnected(); } } //连接SFTP
public bool Connect()
{
try
{
if (!Connected)
{
m_session.connect();
m_channel = m_session.openChannel("sftp");
m_channel.connect();
m_sftp = (ChannelSftp)m_channel;
}
return true;
}
catch (Exception ex)
{
throw ex;
}
} //断开SFTP
public void Disconnect()
{
if (Connected)
{
m_channel.disconnect();
m_session.disconnect();
}
} //SFTP存放文件
public bool Put(string localPath, string remotePath)
{
try
{
Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(localPath);
Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(remotePath);
m_sftp.put(src, dst);
return true;
}
catch (Exception ex)
{
throw ex;
}
} //SFTP获取文件
public bool Get(string remotePath, string localPath)
{
try
{
Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(remotePath);
Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(localPath);
m_sftp.get(src, dst);
return true;
}
catch
{
return false;
}
}
//删除SFTP文件
public bool Delete(string remoteFile)
{
try
{
m_sftp.rm(remoteFile);
return true;
}
catch
{
return false;
}
} //获取SFTP文件列表
public ArrayList GetFileList(string remotePath, string fileType)
{
try
{
Tamir.SharpSsh.java.util.Vector vvv = m_sftp.ls(remotePath);
ArrayList objList = new ArrayList();
foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry qqq in vvv)
{
string sss = qqq.getFilename();
if (sss.Length > (fileType.Length + ) && fileType == sss.Substring(sss.Length - fileType.Length))
{ objList.Add(sss); }
else { continue; }
} return objList;
}
catch
{
return null;
}
} //登录验证信息
public class MyUserInfo : UserInfo
{
String passwd;
public String getPassword() { return passwd; }
public void setPassword(String passwd) { this.passwd = passwd; } public String getPassphrase() { return null; }
public bool promptPassphrase(String message) { return true; } public bool promptPassword(String message) { return true; }
public bool promptYesNo(String message) { return true; }
public void showMessage(String message) { Console.WriteLine(message); } }
}

clsSFTPHelper.cs

Program.cs

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SFTPTest
{
class Program
{
static void Main(string[] args)
{
clsSFtpUpload objSFtpUpload = new clsSFtpUpload();
objSFtpUpload.SFtpUpload();
}
} class clsSFtpUpload
{
public bool SFtpUpload()
{
bool bolReturn = false;
string strFileName = @"D:\test.edi"; try
{
clsSFTPHelper objSFtp = new clsSFTPHelper("SFtp IP", "UserName", "PAW", );
//SFtp IP:服务器的IP,UserName:登录名,PAW:密码
objSFtp.Connect();
if (objSFtp.Connected)
{
Console.Write("Connect Succeed!");
if(objSFtp.Get("/test/test.edi", strFileName))//down Get(RemotePath,LocalPath)
{
Console.Write("Download Succeed!");
}
if (objSFtp.Put(strFileName,"/test/test.edi"))//upload Put(LocalPath,RemotePath)
{
Console.Write("Download Succeed!");
}
}
objSFtp.Disconnect(); bolReturn = true;
}
catch (Exception ex)
{
Console.Write("Exception: " + ex.ToString());
bolReturn = false;
}
return bolReturn;
}
}
}

Program.cs

上一篇:动态树Link-cut tree(LCT)总结


下一篇:Java中遍历Map的四种方式