最近遇到一个需求,使用SFTP上传下载文件,由于安全性,需要使用内部代理,在网上找了下,未找到相关代码。就自己整理了一份,实现原理基于
Tamir.SharpSsh.jsch; 部分代码仅供参考。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Tamir.SharpSsh;
using Tamir.SharpSsh.jsch;
namespace GetFileFromFTP
{
class SFTPHelper
{
private Session m_session;
//代理地址
string ProxyHost = ConfigurationSettings.AppSettings["ProxyHost"].ToString();
//代理端口
string ProxyPort = ConfigurationSettings.AppSettings["ProxyPort"].ToString();
//代理用户名
string ProxyUser = ConfigurationSettings.AppSettings["ProxyUser"].ToString();
//代理密码
string ProxyPwd = ConfigurationSettings.AppSettings["ProxyPwd"].ToString();
private Channel m_channel;
private ChannelSftp m_sftp;
private static readonly string defRemotePath = "/";//默认操作是都是从根目录开始。
//host:sftp地址 user:用户名 pwd:密码
public SFTPHelper(string ip, string port, string user, string pwd)
{
try
{
int serverport = Int32.Parse(port);
JSch jsch = new JSch();
m_session = jsch.getSession(user, ip, serverport);
ProxyHTTP proxyhttp = new ProxyHTTP(ProxyHost, Convert.ToInt32(ProxyPort));
proxyhttp.setUserPasswd(ProxyUser, ProxyPwd);
m_session.setProxy(proxyhttp);
MyUserInfo ui = new MyUserInfo();
ui.setPassword(pwd);
m_session.setUserInfo(ui);
}
catch (Exception ex)
{
}
}
//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)
{
return false;
}
}
//断开SFTP
public void Disconnect()
{
if (Connected)
{
m_channel.disconnect();
m_session.disconnect();
}
}
//SFTP存放文件
public bool Put(string localPath, string remotePath)
{
try
{
Connect();
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)
{
WriteLog("文件:" + localPath + "上传失败,失败原因:" + ex.Message + "--" + ex.StackTrace);
return false;
}
return false;
}
/// <summary>
/// 记录日志
/// </summary>
private void WriteLog(string msg)
{
string sql = "INSERT INTO [dbo].[O_Log] ([L_Type],[UserID],[CreateTime])VALUES('" + msg + "','admin','" + DateTime.Now + "'";
SqlHelper.ExecuteSql(sql);
}
//SFTP获取文件
public bool Get(string remotePath, string localPath)
{
try
{
//string fullRemotePath = defRemotePath + remotePath.TrimStart('/');
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;
}
}
//删除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 + 1) && 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) { }
}
}