C#对接sftp(项⽬中⽤到)在项⽬中⽤到的是获取⽂件列表和下载⽂件,这个是可以的,其他应该也没问题
在对接sftp时必须引⽤的三个dll⽂件,可以从newGet包管理⾥⾯下载
DiffieHellman
Org.Mentalis.Security
Tamir.SharpSSH
我们是做了⼀个exe⽤sqlserver的job进⾏调⽤的
//sftp信息
static string strFileName = ConfigurationSettings.AppSettings["FileUrl"];
static string SftpFilepath = ConfigurationSettings.AppSettings["SftpFilepath"];
static string ftpid = ConfigurationSettings.AppSettings["SftpServerIp"];
static string userName = ConfigurationSettings.AppSettings["SftpUserName"];
static string UserPwd = ConfigurationSettings.AppSettings["SftpUserPwd"];
static string conString = ConfigurationSettings.AppSettings["conString"];
static string DMTconString = ConfigurationSettings.AppSettings["DMTconString"];
//邮件⽤到
static SmtpContext context = new SmtpContext();
static string FromMial = ConfigHelper.GetConfigStr("FromMial");//发送⽅
static string ToMial = ConfigHelper.GetConfigStr("ToMial");//接收者
static string CCMial = ConfigHelper.GetConfigStr("CCMial");//抄送者
static string dir = AppDomain.CurrentDomain.BaseDirectory;
static string SFTPfile = ConfigHelper.GetConfigStr("SFTPfile");
static void Main(string[] args)
{
context.Server = ConfigHelper.GetConfigStr("EmailServerAddress");//smtp地址
context.Port = ConfigHelper.GetConfigStr("EmailServerPort").CastToInt32();//端⼝号
context.UserName = ConfigHelper.GetConfigStr("UserName");//UserAccount
context.Password = ConfigHelper.GetConfigStr("Password");//PWD
SftpBll sftpBll = new SftpBll();
try
{
//⽂件列表
ArrayList FileList = sftpBll.FileList(conString, SftpFilepath);
if (FileList.Count > 0)
{
//从sftp上下载⽂件
sftpBll.SFtpDownload(FileList, SftpFilepath, ftpid, userName, UserPwd);
//将获取到的⽂件写⼊数据库
int n = sftpBll.AddFileRecord(conString, FileList, strFileName);
}
}
catch (Exception ex)
{
}
}
关于sftp的的bll做⼀些处理,调⽤helper
public class SftpBll
{
static string ftpid = ConfigHelper.GetConfigStr("SftpServerIp");
static string userName = ConfigHelper.GetConfigStr("SftpUserName");
static string UserPwd = ConfigHelper.GetConfigStr("SftpUserPwd");
static string FileUrl = ConfigHelper.GetConfigStr("FileUrl");
static string FileName = ConfigHelper.GetConfigStr("FileName");
clsSFTPHelper objSFtp = new clsSFTPHelper(ftpid, userName, UserPwd, 22);
///<summary>
///从sftp上下载⽂件
/
//</summary>
///<param name="strFileName"></param>
///<param name="SftpFilepath"></param>
///<param name="ftpid"></param>
///<param name="userName"></param>
///<param name="UserPwd"></param>
///<returns></returns>
public void SFtpDownload(ArrayList FileList, string SftpFilepath, string ftpid, string userName, string UserPwd)
{
//SFtp IP:服务器的IP,UserName:登录名,PAW:密码
objSFtp.Connect();
if (objSFtp.Connected)
{
//获取⽂件列表
foreach (var item in FileList)
{
Log.CreateLogManager().Debug(SftpFilepath + "/" + item.ToString() + FileUrl);                    objSFtp.Get(SftpFilepath + "/" + item.ToString(), FileUrl);
}
}
objSFtp.Disconnect();
}
///<summary>
///移动⽂件
///</summary>
///<param name="sourcePath"></param>
///<param name="destPath"></param>
///<returns></returns>
public ArrayList MoveFile(string sourcePath, string destPath)
{
ArrayList existFile = new ArrayList();
destPath += DateTime.Now.ToString("yyyyMMddhhmmss");
if (Directory.Exists(sourcePath))
{
if (!Directory.Exists(destPath))
{
//⽬标⽬录不存在则创建
try
{
Directory.CreateDirectory(destPath);
}
catch (Exception ex)
{
throw new Exception("创建⽬标⽬录失败:" + ex.Message);
}session下载
}
//获得源⽂件下所有⽂件
List<string> files = new List<string>(Directory.GetFiles(sourcePath));
files.ForEach(c =>
{
string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
if (File.Exists(destFile))
{
existFile.Add(destFile);
}
else
{
File.Move(c, destFile);
}
});
}
return existFile;
}
}
}
关于sftp的helper
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 = Session(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)
{
t();
m_channel = m_session.openChannel("sftp");
t();
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);
(src, dst);
return true;
}
catch (Exception ex)
{
Log.CreateLogManager().Debug(ex.Message);
return false;
}
}
//删除SFTP⽂件
public bool Delete(string remoteFile)
{
try
{
(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 = Filename();
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) { Console.WriteLine(message); }    }
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。