package com.cv.sftp.key.auth;
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import org.apache.commons.io.FileUtils;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
/**
* @author Chandra Vardhan
*
*/
public class SFTPConnection {
public static void main(String[] args) {
SFTPConnection fs = new SFTPConnection();
String authMode = AppInit.authMode;
if (authMode != null && authMode.equalsIgnoreCase("key")) {
fs.jschKeyAuthentication();
} else {
// fs.sendViaJschBasicAuth();
fs.jschKeyAuthentication();
}
}
public void jschKeyAuthentication() {
JSch jsch = new JSch();
Session session = null;
ChannelSftp channel = null;
try {
if (AppInit.url == null) {
throw new RuntimeException("Key file key not found in classpath");
}
URI keyFileURI = AppInit.url.toURI();
jsch.addIdentity(new File(keyFileURI).getAbsolutePath());
session = jsch.getSession(AppInit.keyAuthUserId, AppInit.keyAuthAddress, 22);
if (session != null) {
session.setServerAliveCountMax(5);
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(1000);
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
try {
session.connect();
} catch (JSchException e) {
e.printStackTrace();
}
channel = (ChannelSftp) session.openChannel("sftp");
if (channel != null) {
channel.connect();
try {
File[] listOfFiles = new File(AppInit.keyAuthSourceDirectory).listFiles();
if (listOfFiles != null && listOfFiles.length > 0) {
channel.cd(AppInit.keyAuthDestinationDirectory);
for (File file : listOfFiles) {
if (file.isFile()) {
try {
channel.put(new FileInputStream(file), file.getName());
String backUpDir = AppInit.keyAuthBackupDirectory;
File backDir = new File(backUpDir);
if (backDir != null && !backDir.exists()) {
backDir.mkdirs();
}
try {
FileUtils.copyFileToDirectory(file, backDir);
} catch (Exception e) {
EmailUtility.sendEmail("Problem while taking the file backup !!!",
"There is a problem while taking the file backup !!!");
e.printStackTrace();
}
try {
FileUtils.forceDelete(file);
} catch (Exception e) {
EmailUtility.sendEmail("Problem while deleting the file from source",
"There is a problem while deleting the file from source directory.");
e.printStackTrace();
}
} catch (Exception e) {
EmailUtility.sendEmail("Problem while accessing destination directory of SFTP",
"There is a problem while accessing the destination directory. Host IP : "
+ AppInit.keyAuthAddress + " and User id : "
+ AppInit.keyAuthUserId);
e.printStackTrace();
}
}
}
} else {
EmailUtility.sendEmail(
"There are no files available in the " + AppInit.keyAuthSourceDirectory
+ " directory!!!",
"There are no files available in the " + AppInit.keyAuthSourceDirectory
+ " directory!!!" + AppInit.keyAuthAddress + " and User id : "
+ AppInit.keyAuthUserId);
System.err.println("There are no files available in the " + AppInit.keyAuthSourceDirectory
+ " directory!!!");
}
} catch (Exception e) {
EmailUtility.sendEmail("Problem while accessing source directory of SFTP",
"There is a problem while accessing the Source directory. Host IP : "
+ AppInit.keyAuthAddress + " and User id : " + AppInit.keyAuthUserId);
e.printStackTrace();
}
}
}
} catch (Exception e) {
EmailUtility.sendEmail("Problem while connecting to SFTP",
"There is a problem while getting the SFTP connection. Host IP : " + AppInit.keyAuthAddress
+ " and User id : " + AppInit.keyAuthUserId);
e.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
} |
No comments:
Post a Comment