简体   繁体   English

如何判断文件是否已完成FTP?

[英]How can I tell if a file has finished being FTPed?

We have a C# Windows service polling a folder waiting for an FTP'ed file to be posted in. To avoid using the file when it is still being written to we attempt to get a lock on the file first, however, there seems to be occasions where we are getting a lock on the file after the FTP'ed file is created but before the file is written to, so we end up opening an empty file. 我们有一个C#Windows服务轮询一个等待FTP文件发布的文件夹。为了避免在文件仍被写入时使用该文件,我们首先尝试锁定文件,但是,似乎有在创建FTP文件之后但在写入文件之前我们获取文件锁定的情况,因此我们最终打开一个空文件。

Is there a reliable anyway to tell if the FTP is complete? 有没有可靠的判断FTP是否完整?

You could possibly change the filename before upload, then rename it after it's done. 您可以在上传之前更改文件名,然后在完成后重命名。 that way it will look like it doesn't exist until finished. 那样它看起来好像直到完成才存在。

A practice I've seen done is you transfer two files, One which is the actual file, then a second one which will we can call a .done file. 我见过的一种做法是你传输两个文件,一个是实际文件,然后是另一个我们可以调用.done文件的文件。 The ideal is as soon as you see the .done file you know the first file should be done. 理想的是,只要您看到.done文件就知道第一个文件应该完成。

Other options include watching the file for modifications and wait for a certain ammount of time of no modifications. 其他选项包括观察文件进行修改并等待一段时间没有修改。 Of course this is not full proof. 当然这不是完全证据。

Edit 编辑

Kyle makes a good point that adding a checksum to the .done file and/or indicating the size of the first file is a good protection against fringe cases. Kyle明确指出,在.done文件中添加校验和和/或指示第一个文件的大小是对边缘情况的良好保护。

I'm always a big fan of the .filepart protocol, so that no matter what transfer protocol you use (ftp,ssh,rsync,etc) you have the same understanding. 我一直是.filepart协议的忠实粉丝,所以无论你使用什么传输协议(ftp,ssh,rsync等),你都有相同的理解。

This isn't a direct answer to your question, but instead of searching for a ftp-only solution a more generic solution could be better for you in long run. 这不是您问题的直接答案,但不是搜索仅支持ftp的解决方案,从长远来看,更通用的解决方案对您来说可能更好。

(.filepart: rename the file,test.txt to test.txt.filepart, then when it is done, name it back to test.txt) (.filepart:将文件重命名为test.txt到test.txt.filepart,然后在完成后,将其命名为test.txt)

What about using a folder watcher to index the contents and if a files size does not change within 5 mins you can pretty-much guarantee the upload has been finished. 如何使用文件夹观察器索引内容,如果文件大小在5分钟内没有变化,你可以非常保证上传已经完成。

The time out could be tied to the timeout of your FTP server to. 超时可能与FTP服务器的超时有关。

http://www.codeproject.com/KB/files/MonitorFolderActivity.aspx http://www.codeproject.com/KB/files/MonitorFolderActivity.aspx

I've always used a checksum file. 我一直使用校验和文件。 So you send a checksum file that denotes the filesize and the checksum. 因此,您发送一个校验和文件,表示文件大小和校验和。 You'll know the file is uploaded correctly when the checksum in the first file matches the actual checksum on the file system. 当第一个文件中的校验和与文件系统上的实际校验和匹配时,您将知道文件已正确上载。

The method I've used in the past is a mix of some of the other replies here. 我过去使用的方法是这里的一些其他回复的混合。 ie FTP a file using a different extension to the one expected (eg FILENAME.part) then rename it with the proper extension as the last step of uploading. 即使用与预期的扩展名不同的扩展名(例如FILENAME.part)来FTP文件,然后使用适当的扩展名将其重命名为上传的最后一步。

On the server, use a FileSystemWatcher to look for new files with the correct extension. 在服务器上,使用FileSystemWatcher查找具有正确扩展名的新文件。 The FSW will not see the file until it's renamed, and the renaming operation is atomic so the file will be complete and available the moment it's been renamed. FSW在重命名之前不会看到该文件,并且重命名操作是原子的,因此文件将在重命名时完成并可用。

Renaming or moving files of course relies on you having control over the uploading process. 重命名或移动文件当然取决于您是否可以控制上传过程。

If you do not have any control over how the files are uploaded, you will be stuck with using the FSW to know a file is being uploaded, then monitoring it's size - when it's unchanged for a long period of time you may be able to assume it's complete. 如果您无法控制文件的上传方式,那么您将无法使用FSW知道正在上传的文件,然后监控文件的大小 - 当它长时间保持不变时,您可以假设它完整​​了。

您可能希望查看System.IO.FileSystemWatcher而不是轮询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何判断一个数字何时完成增加? - How can I tell when a number has finished increasing? 如何判断Microsoft.WindowsAzure.Storage.DataMovement.TransferManager的CopyDirectoryAsync完成了? - How can I tell when CopyDirectoryAsync from Microsoft.WindowsAzure.Storage.DataMovement.TransferManager has finished? NetworkStream.Write 立即返回 - 我如何知道它何时完成发送数据? - NetworkStream.Write returns immediately - how can I tell when it has finished sending data? Gridview,有一个事件,我如何确定它已完成渲染? - Gridview, is there an event, how would I determine that it has finished being rendered? 如何在不轮询ThreadState的情况下判断线程是否已完成执行? - How can I tell if a thread is finished executing without polling ThreadState? 我可以使用什么事件处理程序来告诉DataGrid控件何时完成排序? - What event handler can I use to tell when a DataGrid control has finished sorting? 关闭 wpf window 时如何判断 await 是否正在运行或已完成运行 - How do I tell if await is running or has finished running when closing wpf window 如何确保我下载的文件已完成? - How do I make sure that the file I download has finished? 如何判断打印机是否打印到文件? - How can I tell if a printer will print to file? 我如何 %100 确定已在 C# 中完成了对文本文件的写入,以便我可以继续执行其他操作? - How can I be %100 sure that writing to a text file has been finished in C#, so that I can proceed with another operation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM