如何检测文件正在通过 FTP 上传 [英] How to detect that a file is being uploaded over FTP

查看:53
本文介绍了如何检测文件正在通过 FTP 上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在监视一组用户可以上传文件的文件夹.当文件上传完成时,我必须应用处理,但我不知道如何检测文件未完成上传.

My application is keeping watch on a set of folders where users can upload files. When a file upload is finished I have to apply a treatment, but I don't know how to detect that a file has not finish to upload.

有什么方法可以检测文件是否尚未被 FTP 服务器释放?

Any way to detect if a file is not released yet by the FTP server?

推荐答案

这个问题没有通用的解决方案.

There's no generic solution to this problem.

某些 FTP 服务器会锁定正在上传的文件,阻止您在文件仍在上传时访问它.例如 IIS FTP 服务器就是这样做的.大多数其他 FTP 服务器没有.在防止文件在上传时被访问,请参阅我的回答.

Some FTP servers lock the file being uploaded, preventing you from accessing it, while the file is still being uploaded. For example IIS FTP server does that. Most other FTP servers do not. See my answer at Prevent file from being accessed as it's being uploaded.

该问题有一些常见的解决方法(最初发布于 SFTP 文件锁定机制,但也与 FTP 相关)):

There are some common workarounds to the problem (originally posted in SFTP file lock mechanism, but relevant for the FTP too):

  • 您可以让客户上传一个完成"的文件上传完成后的文件.让您的自动化系统等待完成"文件出现.

  • You can have the client upload a "done" file once the upload finishes. Make your automated system wait for the "done" file to appear.

你可以有一个专门的上传"文件夹并让客户端(以原子方式)将上传的文件移动到完成"文件夹.让您的自动化系统看起来完成"仅文件夹.

You can have a dedicated "upload" folder and have the client (atomically) move the uploaded file to a "done" folder. Make your automated system look to the "done" folder only.

为正在上传的文件(.filepart")制定文件命名约定,并让客户端(以原子方式)在上传后将文件重命名为其最终名称.让您的自动化系统忽略.filepart"文件.

Have a file naming convention for files being uploaded (".filepart") and have the client (atomically) rename the file after upload to its final name. Make your automated system ignore the ".filepart" files.

参见(我的)文章在上传/上传到临时文件名时锁定文件实施此方法的示例.

See (my) article Locking files while uploading / Upload to temporary file name for an example of implementing this approach.

此外,一些 FTP 服务器内置了此功能.例如 ProFTPD 及其 HiddenStores 指令.

Also, some FTP servers have this functionality built-in. For example ProFTPD with its HiddenStores directive.

一个粗略的黑客是定期检查文件属性(大小和时间)并认为上传完成,如果属性在一段时间内没有改变.

A gross hack is to periodically check for file attributes (size and time) and consider the upload finished, if the attributes have not changed for some time interval.

您还可以利用某些文件格式具有明确的文件结束标记(如 XML 或 ZIP)这一事实.所以你知道,文件是不完整的.

You can also make use of the fact that some file formats have clear end-of-the-file marker (like XML or ZIP). So you know, that the file is incomplete.

某些 FTP 服务器允许您配置在上传完成时调用的挂钩.你可以利用它.例如 ProFTPD 有一个 mod_exec 模块(参见 ExecOnCommand代码>指令).

Some FTP servers allow you to configure a hook to be called, when an upload is finished. You can make use of that. For example ProFTPD has a mod_exec module (see the ExecOnCommand directive).

这篇关于如何检测文件正在通过 FTP 上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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