//linux服务器ip
private static string host = Appsettings.app(new string[] { "FTPConfig", "FtpIp" });
//linux服务器登录名
private static string username = Appsettings.app(new string[] { "FTPConfig", "UserName" });
//linux服务器密码
private static string password = Appsettings.app(new string[] { "FTPConfig", "Password" });
//linux服务器文件存放路径
private static string UploadPath = Appsettings.app(new string[] { "FTPConfig", "UploadPath" });
//linux服务器端口
private static int Port = Appsettings.app(new string[] { "FTPConfig", "Port" }).CastToInt32();
/// <summary>
/// 文件下载
/// </summary>
/// <param name="Path">文件地址</param>
/// <returns></returns>
public static byte[] Download(string Path)
{
byte[] bytes = null;
try
{
var connectionInfo = new Renci.SshNet.ConnectionInfo(host, Port, username, new PasswordAuthenticationMethod(username, password));
using (var sftp = new SftpClient(connectionInfo))
{
sftp.Connect();
bytes = sftp.ReadAllBytes(Path);
sftp.Disconnect();
}
return bytes;
}
catch (Exception ex)
{
return null;
}
}
注意:1、需要NuGet包SSH.NET 2、Linux服务器要安装SSH