public virtual void FtpDropOff(string fileDirectory, string fileName, Dictionary<string, string> config)
{
ChannelSftp channelSftp;
var url = config["sftpUrl"];
var username = config["sftpUsername"];
var password = config["sftpPassword"];
var knownHostsFile = config["knownHostsFile"];
var filePath = fileDirectory + fileName;
var printxml = config["printxml"] == "true";
if (printxml)
{
Console.WriteLine("Sftp Url: " + url);
Console.WriteLine("Username: " + username);
//Console.WriteLine("Password: " + password);
Console.WriteLine("Known hosts file path: " + knownHostsFile);
}
var jsch = new JSch();
if (printxml)
{
// grab the contents fo the knownhosts file and print
var hostFile = File.ReadAllText(knownHostsFile);
Console.WriteLine("known host contents: " + hostFile);
}
jsch.setKnownHosts(knownHostsFile);
// setup for diagnostic
// Get the KnownHosts repository from JSchs
var hkr = jsch.getHostKeyRepository();
var hks = hkr.getHostKey();
HostKey hk;
if (printxml)
{
// Print all knownhosts and keys
if (hks != null)
{
Console.WriteLine();
Console.WriteLine("Host keys in " + hkr.getKnownHostsRepositoryID() + ":");
foreach (var t in hks)
{
hk = t;
Console.WriteLine("local HostKey host: <" + hk.getHost() + "> type: <" + hk.getType() + "> fingerprint: <" + hk.getFingerPrint(jsch) + ">");
}
Console.WriteLine("");
}
}
var session = jsch.getSession(username, url);
session.setPassword(password);
try
{
session.connect();
// more diagnostic code for troubleshooting sFTP connection errors
if (printxml)
{
// Print the host key info of the connected server:
hk = session.getHostKey();
Console.WriteLine("remote HostKey host: <" + hk.getHost() + "> type: <" + hk.getType() + "> fingerprint: <" + hk.getFingerPrint(jsch) + ">");
}
var channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
}
catch (SftpException e)
{
throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection", e);
}
catch (JSchException e)
{
throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection", e);
}
try
{
if (printxml)
{
Console.WriteLine("Dropping off local file " + filePath + " to inbound/" + fileName + ".prg");
}
channelSftp.put(filePath, "inbound/" + fileName + ".prg", ChannelSftp.OVERWRITE);
if (printxml)
{
Console.WriteLine("File copied - renaming from inbound/" + fileName + ".prg to inbound/" + fileName + ".asc");
}
channelSftp.rename("inbound/" + fileName + ".prg", "inbound/" + fileName + ".asc");
}
catch (SftpException e)
{
throw new LitleOnlineException("Error occured while attempting to upload and save the file to SFTP", e);
}
channelSftp.quit();
session.disconnect();
}