I'm go through the SO to find the solution, but i am not able to get proper solution. Actually I got the following program from copy file from SFTP server to local:
public class SFTP_logic {
public static void main(String args[]) {
String Username = "abc";
String Host = "ftp.abc.com";
int port = 22;
String Password="abc123";
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(Username, Host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(Password);
session.connect();
System.out.println("connected..");
Channel channel = session.openChannel("sftp");
channel.connect();
// System.out.println("---------------------Connected to SFTP server.----------------------");
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.get("abc_20161031.txt.xyz", "localfile.txt");
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
My Requirement :- I need to polling SFTP server's directory every 10 minutes, Is there any new file, if yes then have to copy that file to local.
So, my problem is i want to poll SFTP server's directory using JAVA. so how i can do that? i find one solution which point to this. but it contain lots of class and it's too confusing, because same polling i am able to do for local machine using below program.
public class MonitorDirectory {
public static void main(String[] args) throws IOException,
InterruptedException {
Path faxFolder = Paths.get("C:\\Users\\Yash\\Documents");
WatchService watchService = FileSystems.getDefault().newWatchService();
faxFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
boolean valid = true;
do {
WatchKey watchKey = watchService.take();
for (WatchEvent event : watchKey.pollEvents()) {
WatchEvent.Kind kind = event.kind();
if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
String fileName = event.context().toString();
System.out.println("File Created:" + fileName);
}
}
valid = watchKey.reset();
} while (valid);
}
}
So, is there any code which works for SFTP directory fetching like above work for local.