简体   繁体   中英

Remote directory monitor and transfer files in Java

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM