Java:使用 SFTP 检查文件是否已完全复制 [英] Java: check whether a file has been fully copied using SFTP
问题描述
我每 35 秒在某个目录上运行一个轮询器.这些文件通过 SFTP 服务器放置在此目录中.问题是每当轮询与复制文件的时间发生冲突时.它还会选择尚未完全复制的不完整文件.
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?
推荐答案
文件观察者有几种常见的策略可以知道"一个文件已经完全传输
There are several common strategies for file watchers to "know" a file is completely transferred
按时间间隔轮询,如果文件大小在一个时间间隔内没有变化,则将文件视为完全传输.例如每 1 分钟观察一次文件是否存在.一旦您看到文件存在,请每 5 秒监视其大小.如果文件大小在 30 秒内保持不变,则将其视为完全传输.
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.
让传输过程在文件传输后创建一个标记文件.例如完成传输文件FOO.txt
后,创建一个空的FOO.txt.tag
.您的文件观察者将检查 FOO.txt.tag
是否存在,一旦存在,您就知道 FOO.txt
已完全转移
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:
- 方法 1 对传输过程的影响最小.有时文件是由 3rd 方传输的,您几乎无法像方法 2 那样告诉他们创建标记文件.但是您可以看出这种方法不是 100% 可靠的,尤其是在网络较差的情况下.
- 方法 2 是最可靠的.但是,如前所述,有些情况下您无法控制转移过程
- 方法三只适用于特殊情况
选择适合您的需求
这篇关于Java:使用 SFTP 检查文件是否已完全复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!