• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you tell if entire file is uploaded?

 
Chris Ward
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am needing to pick up files from a FTP or WebDAV location, but I need to make sure the entire file has been placed on the server before processing. I am checking the folder on a timer every 3-5 min or so.

When I do a File.exists() it tells me that the file exists as soon as the transfer starts, as that is when the file system creates the file, but it is not really when the 'real' file exists. If I try to operate on the file before it has finished being uploaded, I get garbage, or IO errors.

Any ideas?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

Welcome to JavaRanch!

This is a tricky problem. One good solution, the one taken by the Mozilla browser for downloaded files, is to give the partial download a different name, and only rename it to the final name when its complete. A similar thing is to save the partial file in an alternate location and move it when its complete. These are both steps that the downloading software have to take which then make the file monitor's job easy.

If you have no control over the actual download software, though, it's a lot trickier. You can watch the files and only try to process them if their last modified time hasn't changed for some period of time (say, 1 minute.) If you have checksums or MD5 hashes, you could compute and check those.
 
Chris Ward
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I won't have control of the file names, as ftp and webdav will be used to get the files to the computer. I guess I will need to look at the modified time on the file. Does that attribute change as the file is showing up on the computer? Or am I just waiting some arbitrary time from file creation to assume the file 'must be here by now'.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like the rename trick. Another trick is to upload the real file and then upload a 1-byte flag file. When the flag file is present, you know the real file is done. If you don't control the whole process you might not be able to make that work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!