package com.cv.sftp.write;
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 SFTPService {
public static void main(String[] args) {
SFTPService fs = new SFTPService();
String authMode = AppInit.authMode;
if (authMode != null && authMode.equalsIgnoreCase("basic")) {
fs.sendViaJschBasicAuth();
} else if (authMode != null && authMode.equalsIgnoreCase("key")) {
fs.sendViaJschKeyAuth();
} else {
// fs.sendViaJschBasicAuth();
fs.sendViaJschKeyAuth();
}
}
public void sendViaJschBasicAuth() {
JSch conn = new JSch();
Session session = null;
ChannelSftp channel = null;
try {
session = conn.getSession(AppInit.basicAuthUserId, AppInit.basicAuthAddress);
if (session != null) {
session.setPassword(AppInit.basicAuthPassword);
session.setServerAliveCountMax(10);
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(150000);
session.connect();
channel = (ChannelSftp) session.openChannel("sftp");
if (channel != null) {
channel.connect();
try {
File[] listOfFiles = new File(AppInit.basicAuthSourceDirectory).listFiles();
if (listOfFiles != null && listOfFiles.length > 0) {
for (File file : listOfFiles) {
if (file.isFile()) {
try {
channel.put(new FileInputStream(file),
AppInit.basciAuthDestinationDirectory + file.getName());
String backUpDir = AppInit.basciAuthBackupDirectory;
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.basicAuthAddress + " and User id : "
+ AppInit.basicAuthUserId);
e.printStackTrace();
}
}
}
} else {
EmailUtility.sendEmail(
"There are no files available in the " + AppInit.basicAuthSourceDirectory
+ " directory!!!",
"There are no files available in the " + AppInit.basicAuthSourceDirectory
+ " directory!!!" + AppInit.basicAuthAddress + " and User id : "
+ AppInit.basicAuthUserId);
System.err.println("There are no files available in the " + AppInit.basicAuthSourceDirectory
+ " 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.basicAuthAddress + " and User id : " + AppInit.basicAuthUserId);
e.printStackTrace();
}
} else {
EmailUtility.sendEmail("Problem while connecting to SFTP",
"There is a problem while getting the SFTP connection. Host IP : "
+ AppInit.basicAuthAddress + " and User id : " + AppInit.basicAuthUserId);
System.err.println("Problem while getting the SFTP channel!");
}
}
} catch (Exception e) {
EmailUtility.sendEmail("Problem while connecting to SFTP",
"There is a problem while getting the SFTP connection. Host IP : " + AppInit.basicAuthAddress
+ " and User id : " + AppInit.basicAuthUserId);
e.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
public void sendViaJschKeyAuth() {
JSch jsch = new JSch();
Session session = null;
ChannelSftp channel = null;
try {
if (AppInit.url == null) {
throw new RuntimeException("Key file auth-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