Solved

Detecting if an FTP file is being written to before attempting download

Posted on 2008-10-02
5
273 Views
Last Modified: 2013-12-29
Hi,
I am polling an IIS FTP Server for files on a regular basis using Java.

Sometimes i am receiving an IOException, because the file is being written to while i am reading it.

Using a "ready" or trigger file is not an option at the moment with the vendor.

I am using Java SDK 1.4.2 and Apache Commons Net 1.4.1.

I am dealing with large files, in the code snippet i copy the stream to a ByteArrayOutputStream. The error occurs in the copyStream method while reading in the file.

Does anyone have any suggestions? i guess i could retry a few times...

Thanks

Mike


try {
 
            ftp = connect();
 
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
 
            inputStream = new BufferedInputStream(ftp.retrieveFileStream(srcfilename));
 
            byteOutputStream = new ByteArrayOutputStream();
 
            /** Thread safe byte copy * */
            FTPUtil ecfu = new FTPUtil();
            ecfu.copyStream(inputStream, byteOutputStream);
 
            ftp.completePendingCommand();
            logger.debug("FTP: Byte transfer from server sucessful");
 
        } catch (IOException e) {
            throw new FTPServiceException("FTP: IOException downloading " + srcfilename, e);
        }

Open in new window

0
Comment
Question by:kwikksilva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
mbodewes earned 125 total points
ID: 22626479
Not really, afaik there is no option in the FTP protocol that tells you it is done. I've had the same problem, but then with detecting it after delivery by FTP.

You could try and see if the server understands this command:


MDTM

Syntax: MDTM remote-filename

Returns the last-modified time of the given file on the remote host in the format "YYYYMMDDhhmmss": YYYY is the four-digit year, MM is the month from 01 to 12, DD is the day of the month from 01 to 31, hh is the hour from 00 to 23, mm is the minute from 00 to 59, and ss is the second from 00 to 59.


And wait during polling until it is hasn't been changed for some time.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22630742
Whats the error you are getting? If the file is huge, keeping into memory is not a good idea.
0
 
LVL 9

Expert Comment

by:mbodewes
ID: 22652633
kwikksilva, have you solved the problem? It might be interesting to post the error message so we can exclude other errors.
0
 

Author Closing Comment

by:kwikksilva
ID: 31502314
This is the closest to the correct answer, i used the getTimestamp() method of FTPFile, and then set a parameter called timestamp-offset (in seconds) is my settings file. I don't touch the file unless it is older than the offset. This works for me in a production environment.
0
 

Author Comment

by:kwikksilva
ID: 22653382
Thanks guys, sorry i did not want to post the Exception, as it was only a side effect of the file being in transit. I used a play on mbodewes solution to avoid the problem altogether.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 40
use lov values 2 72
SequenceInputStream example 3 22
wild fly 8 startup error 2 43
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question