0

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.

yash
  • 2,101
  • 2
  • 23
  • 32
  • 1
    The second program monitors a local file system, so it can just ask the Operative System to notify it about any filesystem changes. You want to monitor a remote SFTP server, running on a different filesystem, operative system and machine, so your OS has no idea about changes in it. I don't know of any library to automate that (off the top of my head, I mean), but there's always the dirty option of connecting to the SFTP server every 10 minutes, get a text listing of the files, compare it with the listing you saved 10 minutes ago and look for changes in it. – walen Nov 03 '16 at 15:07
  • @walen, thanks for your idea. it means there's no way to combine this two class such a way that i can poll SFTP server and get the file if anyone is new , but may be i have to follow dirty way as of now. – yash Nov 03 '16 at 16:44
  • Suggestion: Easier way, when your location changes, polling time changes, username password changes, keep it out in configuration, I had similar requirement where we used camel-file2 & solved it with ease & everything was configuration based – Ashish Dec 28 '16 at 19:10

2 Answers2

0

Sftp is a bit more complex then local file system. Hence the solution here https://github.com/drapostolos/rdp4j looks confusing. For the most part, all you care about is the listener class, plug your copyToLocal function in the listener for the events your care for ("Add/Remove"). I feel it's a good solution than anything that's out there.

-3

I wouldn't do it wtih Java... I'd set up a cron job to sync remote to local and just run it every 10 minutes.

ivanivan
  • 2,155
  • 2
  • 10
  • 11