How to detect FTP file transfer completion?

天大地大妈咪最大 提交于 2019-12-10 17:56:56

问题


I am writing a script that polls an FTP site for files and downloads them locally as and when available. The files are deposited to the FTP site randomly by various source parties. I need a way to be able to detect if the file on the FTP site has been transferred over completely by the source party, before downloading them. Any thoughts on how to go about this?

Thanks in advance for any help!


回答1:


If you cannot manipulate the FTP server itself the only way of checking that comes to my mind is polling the filesize and if the filesize doesn't change for a longer time you can be quite sure the upload has finished. But nobody can guarantee. Ideally you can adapt the FTP server and make it execute some script after finishing some upload.

Some pseudo-code:

my %filesizes;
my %processed;

sub poll {
    foreach (@files_on_ftp) {
         if($_->filesize == $filesizes{$_->filename} and not $processed{$_->filename}) {
             process($_);
             $processed{$_->filename)++;
         }
    }
}



回答2:


If you have control over the client, a much safer, cleaner and efficient way is to have the client do the following:

  1. Upload the file to ..../partial/somefile
  2. Rename ..../partial/somefile to ..../complete/somefile

This causes the file to appear in the latter directory all at once, so all you have to do is scan that directory. You could even ask the OS to be notified of additions to that directory if you wanted a non-polling solution.




回答3:


Like ikegami's solution depends mine of client side:

  1. first is file uploaded
  2. if it is completed, client uploads empty flag-file (like file.name.txt.finished)

When you see finished-file, you know file is ready.



来源:https://stackoverflow.com/questions/5625421/how-to-detect-ftp-file-transfer-completion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!