C# Sftp操作
C# Sftp操作
SFTP释义-----引自百度百科
sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的网络的加密方法。
sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的其中一部分,是一种传输档案至 Blogger 伺服器的安全方式。
其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件信息传输子系统,
SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接和答复操作,
所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。
SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。
但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,
如果您对网络安全性要求更高时,可以使用SFTP代替FTP。
准备
Sftp的上传下载等操作用到了C#的第三方类库SharpSSH,需要添加压缩包中的3个dll文件
Tamir.SharpSSH.dll、Org.Mentalis.Security.dll、DiffieHellman.dll 下载地址
SftpHelper类
class sftpHelper
{
private Session m_session;
private Channel m_channel;
private ChannelSftp m_sftp;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ip">sftp地址</param>
/// <param name="user">sftp用户名</param>
/// <param name="pwd">sftp密码</param>
/// <param name="port">端口,默认20</param>
public sftpHelper(string ip, string user, string pwd, string port = "20")
{
int serverport = Int32.Parse(port);
JSch jsch = new JSch();
m_session = jsch.getSession(user, ip, serverport);
MyUserInfo ui = new MyUserInfo();
ui.setPassword(pwd);
m_session.setUserInfo(ui);
}
/// <summary>
/// 连接状态
/// </summary>
public bool Connected { get { return m_session.isConnected(); } }
/// <summary>
/// 连接SFTP
/// </summary>
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)
{
return false;
}
}
/// <summary>
/// 断开SFTP
/// </summary>
public void Disconnect()
{
if (Connected)
{
m_channel.disconnect();
m_session.disconnect();
}
}
/// <summary>
/// SFTP存放文件
/// </summary>
/// <param name="localPath">本地文件路径</param>
/// <param name="remotePath">sftp远程地址</param>
public bool Put(string localPath, string remotePath)
{
try
{
if (this.Connected)
{
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)
{
return false;
}
return false;
}
/// <summary>
/// SFTP获取文件
/// </summary>
/// <param name="remotePath">sftp远程文件地址</param>
/// <param name="localPath">本地文件存放路径</param>
public bool Get(string remotePath, string localPath)
{
try
{
if (this.Connected)
{
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 (Exception ex)
{
return false;
}
return false;
}
/// <summary>
/// 删除SFTP文件
/// </summary>
/// <param name="remoteFile">sftp远程文件地址</param>
public bool Delete(string remoteFile)
{
try
{
if (this.Connected)
{
m_sftp.rm(remoteFile);
return true;
}
}
catch
{
return false;
}
return false;
}
/// <summary>
/// 移动SFTP文件
/// </summary>
/// <param name="currentFilename">sftp远程文件地址</param>
/// <param name="newDirectory">sftp移动至文件地址</param>
public bool Move(string currentFilename, string newDirectory)
{
try
{
if (this.Connected)
{
Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(currentFilename);
Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(newDirectory);
m_sftp.rename(src, dst);
return true;
}
}
catch (Exception ex)
{
return false;
}
return false;
}
/// <summary>
/// 获取SFTP文件列表
/// </summary>
/// <param name="remotePath">sftp远程文件目录</param>
/// <param name="fileType">文件类型</param>
public ArrayList GetFileList(string remotePath, string fileType)
{
try
{
if (this.Connected)
{
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 + 1) && fileType == sss.Substring(sss.Length - fileType.Length))
{ objList.Add(sss); }
else { continue; }
}
return objList;
}
}
catch
{
return null;
}
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) { }
}
使用winform测试
/// <summary>
/// 下载文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
sftpHelper sftp = new sftpHelper("192.168.2.133", "22", "admin", "Admin123");
sftp.Connect();
sftp.Get("/home/20171013103113.csv", dialog.SelectedPath);
sftp.Disconnect();
}
}
/// <summary>
/// 上传测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "|*.csv";
if (dialog.ShowDialog() == DialogResult.OK)
{
sftpHelper sftp = new sftpHelper("192.168.2.133", "22", "admin", "Admin123");
sftp.Connect();
sftp.Put(dialog.FileName, "/home/");
sftp.Disconnect();
}
}
/// <summary>
/// 删除测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
sftpHelper sftp = new sftpHelper("192.168.2.133", "22", "admin", "Admin123");
sftp.Connect();
sftp.Delete("/home/20171013103113.csv");
sftp.Disconnect();
}
/// <summary>
/// 移动测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
sftpHelper sftp = new sftpHelper("192.168.2.133", "22", "admin", "Admin123");
sftp.Connect();
sftp.Move("/home/20171013103113.csv", "/home/temp/20171013103113.csv");
sftp.Disconnect();
}