0

Can anyone please let me know if there is a way to monitor a directory in HostB and transfer new files in that directory to HostA in Java?

FYI, my Java code should reside in HostA.

Rodrigo Taboada
  • 2,727
  • 4
  • 24
  • 27
CentosUser
  • 201
  • 1
  • 4
  • 14
  • possible duplicate of [Java File commands over SSH](http://stackoverflow.com/questions/19930795/java-file-commands-over-ssh) – Davis Broda Mar 10 '15 at 13:19

2 Answers2

0

I would use JSch. If HostA and HostB are linux hosts, this should probably do it. You can do secure copy (scp) to transfer the data and execute some shell code using ssh to monitor the file remotely. This can be done by setting up public/private keys.

To monitor with shell, you could use something like:

#!/bin/bash

# directory to monitor
monitoredDirectory="/tmp";
# control file
monitorFile="$(mktemp).$$"
# sleep time
monitorSleep=10

while /bin/true;
do
  # if you want to see directories remove '-type f'
  find "${monitoredDirectory}" -type f -cnewer "$monitorFile" -print 2>/dev/null
  touch "$monitorFile"
  sleep $monitorSleep
done
krico
  • 5,723
  • 2
  • 25
  • 28
  • Is there anyway that it takes key as authentication method instead of password? Also can you please be more specific on "execute some shell code using ssh to monitor the file remotely" – CentosUser Mar 10 '15 at 13:45
0

You have to be able to have access in HostB via some protocol that there is java code for it (HTTP, samba, ftp, nfs etc). Then you just make a Java daemon thread that checks the directory periodically to see if there are any changes and you copy the data