如何使用FTP在服务器上知道文件是否完整? [英] How to know if file is complete on the server using FTP?

查看:212
本文介绍了如何使用FTP在服务器上知道文件是否完整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java文件扫描器应用程序,它使用FTP来扫描服务器上的目录。获取目录的文件列表并逐一下载。另一方面,在服务器上,有一个过程写入这些文件。如果我很幸运,我不会尝试下载不完整的文件,但是如何确保服务器上的写入过程是否完整,文件句柄是否关闭,文件是否可以下载?



我无法控制在服务器上的写入过程。此外,我没有对该目录的写入权限来尝试获取一个写入句柄,以检查是否已经有一个写入句柄打开了,所以这个选项是关闭的。



是否有解决此问题的FTP功能?

解决方案

这是一个非常老旧的知名问题。



没有办法绝对确定FTP守护程序写入的文件是否完整。甚至文件传输失败,然后重新启动并完成。您必须轮询文件的大小并设置时间限制,例如5分钟。如果在这段时间内大小没有改变,你会认为这个文件是完整的。



如果可能,处理文件的程序应该能够处理部分文件。



更好的选择是rsync,这是更强大和确定性的。甚至可以配置(通过命令行选项)将数据最初写入临时位置,并在成功完成后将其移动到最终的目标路径。如果该文件存在于您期望的位置,则按照定义完成。


I have a file scanner application in Java, that keeps scanning a directory on a server using FTP. gets list of files of the directory and downloads them one by one. on the other side, on the server, there's a process that writes these files. if I'm lucky I wouldn't try to download an incomplete file but how can I make sure if the write process on the server is complete and the file handle is closed, and file is ready to be downloaded?

I have no control on the write process which is on the server. moreover, I don't have write permission on the directory to try to get a write-handle in order to check if there's already a write handle open, so this option is off the table.

Is there an FTP function addressing this problem?

解决方案

This is a very old and well-known problem.

There is no way to be absolutely certain a file being written by the FTP daemon is complete. It's even possible that the file transfer failed and then gets restarted and completed. You must poll the file's size and set a time limit, say 5 minutes. If the size does not change during that time you assume the file is complete.

If possible, the program that processes the file should be able to deal with partial files.

A much better alternative is rsync, which is much more robust and deterministic. It can even be configured (via command-line option) to write the data initially to a temporary location and move it to its final destination path upon successful completion. If the file exists where you expect it, then it is by definition complete.

这篇关于如何使用FTP在服务器上知道文件是否完整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆