Reputation: 189
I have a poller running on a certain directory every 35s. The files are placed in this directory through a SFTP server. The problem is whenever the polling conflicts with the time when a file is being copied. It picks the incomplete file also which is not yet copied completely.
Can we know the status of a file whether it is in copying mode or copied mode?
Upvotes: 2
Views: 2900
Reputation: 679
Us this for Unix if you are transferring files using winscp or FTP:
public static void isFileReady(File entry) throws Exception {
long realFileSize = entry.length();
long currentFileSize = 0;
do {
try (FileInputStream fis = new FileInputStream(entry);) {
currentFileSize = 0;
while (fis.available() > 0) {
byte[] b = new byte[1024];
int nResult = fis.read(b);
currentFileSize += nResult;
if (nResult == -1)
break;
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("currentFileSize=" + currentFileSize + ", realFileSize=" + realFileSize);
} while (currentFileSize != realFileSize);
}
Upvotes: 0
Reputation: 40036
There are several common strategies for file watchers to "know" a file is completely transferred
Poll with time interval, and treat the file to be completely transferred if file size is not changing within an interval. e.g. watch for file existence every 1 minute. Once you see the file exists, monitor its size for every 5 seconds. If file size stays constant for 30 seconds, then treat it as completely transferred.
Have the transfer process create a tagging file after file transfer. e.g. After it completed transferring the file FOO.txt
, create an empty FOO.txt.tag
. Your file watcher is going to check for existence of FOO.txt.tag
and once it exists, you know FOO.txt
has been completely transferred
In some special cases that the file is having special format (e.g. a special footer line) then your file watcher can poll the file and see the last lines, and see if they match with the desired pattern
Each method has its pros and cons:
Choose the one that suit your need
Upvotes: 2
Reputation: 33
Have the poller note file sizes. If the size did not change from one round to the next, the file is done downloading.
Can you influence the SFTP server? Can it create a marker file once the download is complete (e.g. '.thisIsAFile.doc.done
')?
Upvotes: 1