C 连接SFTP

C 连接SFTPSFTPHelper cs 文件 using System using System Collections using System Collections Generic using System Collections Specialized using System Configuratio using System Linq using System Text

SFTPHelper.cs文件:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using Tamir.SharpSsh.jsch;

namespace Pfizer.EDIDataCollection.Framework
{

public class SFTPHelper
{

private Session m_session;
private Channel m_channel;
private ChannelSftp m_sftp;

//host:sftp地址 user:用户名 pwd:密码
public SFTPHelper(string host, string user, string pwd)
{

string[] arr = host.Split(':');
string ip = arr[0];
int port = 22;
if (arr.Length > 1) port = Int32.Parse(arr[1]);

JSch jsch = new JSch();
const string passphrase = "";
const string privateKey = "";
if (!"".Equals(privateKey))
{

if (!"".Equals(passphrase))
{

//设置带口令的密钥
jsch.addIdentity(privateKey, passphrase);
}
else
{

//设置不带口令的密钥
jsch.addIdentity(privateKey);
}
}
//设置密钥和密码
m_session = jsch.getSession(user, ip, port);
MyUserInfo ui = new MyUserInfo();
ui.setPassword(pwd);
m_session.setUserInfo(ui);
}

///
/// 新增构造函数(1个参数)
///

/// sftp服务器配置节点名称
public SFTPHelper(string sftpSectionName)
{

var config = System.Configuration.ConfigurationManager.GetSection(sftpSectionName) as NameValueCollection;
string host = config["host_name"];
string user = config["user_name"];
string password = config["password"];
string passphrase = config["passphrase"];
string privateKey = AppDomain.CurrentDomain.BaseDirectory + config["privateKey"];
string[] arr = host.Split(':');
string ip = arr[0];
int port = 22;
if (arr.Length > 1) port = Int32.Parse(arr[1]);

JSch jsch = new JSch();
//const string passphrase = passphrase;
//const string privateKey = privateKeyPath;
if (!"".Equals(privateKey))
{

if (!"".Equals(passphrase))
{

//设置带口令的密钥
jsch.addIdentity(privateKey, passphrase);
}
else
{

//设置不带口令的密钥
jsch.addIdentity(privateKey);
}
}
//设置密钥和密码
m_session = jsch.getSession(user, ip, port);
MyUserInfo ui = new MyUserInfo();
ui.setPassword(password);
m_session.setUserInfo(ui);

}
public SFTPHelper()
{

var config = ConfigurationManager.GetSection("sftpServer") as NameValueCollection;
string host = config["host_name"];
string user = config["user_name"];
string pwd = config["password"];
string passphrase = config["passphrase"];
string privateKey = AppDomain.CurrentDomain.BaseDirectory + config["privateKey"];
string[] arr = host.Split(':');
string ip = arr[0];
int port = Convert.ToInt32(config["port"] ?? "22");//默认端口为22
if (arr.Length > 1) port = Int32.Parse(arr[1]);
JSch jsch = new JSch();
if (!"".Equals(privateKey))
{

if (!"".Equals(passphrase))
{

//设置带口令的密钥
jsch.addIdentity(privateKey, passphrase);
}
else
{

//设置不带口令的密钥
jsch.addIdentity(privateKey);
}
}
//设置密钥和密码
m_session = jsch.getSession(user, ip, 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)
{

LogHelper.Error("ERROR", ex);
return false;
}
}

//断开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
// {

// return false;
// }
//}

//public bool Put(string localPath)
//{

// try
// {

// var config = ConfigurationManager.GetSection("sftpServer") as NameValueCollection;
// string remotePath = config["upload"];
// 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
// {

// return false;
// }
//}

///
/// 增加配置节
///

///

///

/param>

br /> ///

returns>

/returns>

br /> public bool Put(string localPath, string remotePath, string configSection)

br /> {

br />

br /> try

br /> {

br />

br /> var config = ConfigurationManager.GetSection(configSection) as NameValueCollection;

br /> //string remotePath = config[remotePath];

br /> Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(localPath);

br /> Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(config[remotePath]);

br /> m_sftp.put(src, dst);

br /> return true;

br /> }

br /> catch

br /> {

br />

br /> return false;

br /> }

br /> }

br />

br /> //SFTP获取文件

br /> public bool Get(string remotePath, string localPath)

br /> {

br />

br /> try

br /> {

br />

br /> Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(remotePath);

br /> Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(localPath);

br /> m_sftp.get(src, dst);

br /> return true;

br /> }

br /> catch

br /> {

br />

br /> return false;

br /> }

br /> }

br /> public bool Get(string remotePath, string localPath, string filetype)

br /> {

br />

br /> try

br /> {

br />

br /> var config = ConfigurationManager.GetSection("sftpServer") as NameValueCollection;

br /> remotePath = config["download"];

br /> Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(remotePath + filetype);

br /> Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(localPath);

br /> m_sftp.get(src, dst);

br /> return true;

br /> }

br /> catch

br /> {

br />

br /> return false;

br /> }

br /> }

br /> //删除SFTP文件

br /> public bool Delete(string remoteFile)

br /> {

br />

br /> try

br /> {

br />

br /> m_sftp.rm(remoteFile);

br /> return true;

br /> }

br /> catch

br /> {

br />

br /> return false;

br /> }

br /> }

br /> public bool Delete(string fileType, string filepath)

br /> {

br />

br /> try

br /> {

br />

br /> var config = ConfigurationManager.GetSection("sftpServer") as NameValueCollection;

br /> string remotePath = config[filepath];

br /> m_sftp.rm(remotePath + fileType);

br /> return true;

br /> }

br /> catch

br /> {

br />

br /> return false;

br /> }

br /> }

br />

br /> public bool Delete(string fileType, string filepath, string configSection)

br /> {

br />

br /> try

br /> {

br />

br /> var config = ConfigurationManager.GetSection(configSection) as NameValueCollection;

br /> string remotePath = config[filepath];

br /> m_sftp.rm(remotePath + fileType);

br /> return true;

br /> }

br /> catch

br /> {

br />

br /> return false;

br /> }

br /> }

br />

br /> //获取SFTP文件列表

br /> public ArrayList GetFileList(string filepath, string fileType)

br /> {

br />

br /> try

br /> {

br />

br /> var config = ConfigurationManager.GetSection("sftpServer") as NameValueCollection;

br /> string remotePath = config[filepath];

br /> Tamir.SharpSsh.java.util.Vector vvv = m_sftp.ls(remotePath);

br /> ArrayList objList = new ArrayList();

br /> foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry qqq in vvv)

br /> {

br />

br /> string sss = qqq.getFilename();

br /> if (sss.Length > (fileType.Length + 1) && sss.Contains(fileType))

br /> {

br /> objList.Add(sss); }

br /> else {

br /> continue; }

br /> }

br />

br /> return objList;

br /> }

br /> catch

br /> {

br />

br /> return null;

br /> }

br /> }

br />

br />

br /> //登录验证信息

br /> public class MyUserInfo : UserInfo

br /> {

br />

br /> String passwd;

br /> public String getPassword() {

br /> return passwd; }

br /> public void setPassword(String passwd) {

br /> this.passwd = passwd; }

br />

br /> public String getPassphrase() {

br /> return null; }

br /> public bool promptPassphrase(String message) {

br /> return true; }

br />

br /> public bool promptPassword(String message) {

br /> return true; }

br /> public bool promptYesNo(String message) {

br /> return true; }

br /> public void showMessage(String message) {

br /> }

br /> }

br /> }

br /> }

br />

br /> //首先连接

br /> sftp.Connect()

br />

br /> //连通后可调用其他方法例如

br />

br /> //上传文件 zipPath为上传文件路径 sftpServerVirtualMeeting为

br /> sftp.Put(zipPath, "upload", "sftpServerVirtualMeeting");

br />

br /> //config中配置的上传到sftp地址

br /> //最后关闭连接

br /> sftp.Disconnect();

br />

br />

br />

br /> config 中配置:

br />

sftpServerVirtualMeeting>









//端口号


/sftpServerVirtualMeeting>

密钥文件转换:

需要借用工具PuTTYgen 软件 将ppk文件转换成pem文件后可放入代码中使用(我接触的项目只涉及
到单一sftp密钥上传,如若涉及多个sftp密钥,还请自行百度动态转换ppk格式)

ppk转pem:
https://blog.csdn.net/albertfly/article/details/76401176

编程小号
上一篇 2025-09-06 12:57
下一篇 2025-07-05 15:01

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/137046.html