title: blocking (synchronous) ftp download in java?
body: <p>I'm currently using <em>commons-net</em> library for FTP client in my app. I have to download from remote server some files, by some criteria based on the file name. This is a very simplified and reduced version of my actual code (because I do some checks and catch all possible exceptions), but the essence is there:</p>
  
  <pre><code>//ftp is FTPClient object
  //...
  files = ftp.listFiles();
  for (FTPFile ftpFile : files) {
      String name = ftpFile.getName();
      if(conformsCriteria(name)) {
  
          String path = outDirectory + File.separatorChar + name;
          os = new FileOutputStream(path);
  
          ftp.retrieveFile(name, os);
      }
  }
  </code></pre>
  
  <p>Now, what I noticed is that when I run this code, wait a few seconds, and then plug out network cable, output directory contains some "empty" files plus the files actually downloaded, which leads me to believe that this method is working somewhat asynchronously... But then again, some files are downloaded (size > 0KB), and there are these empty files (size = 0KB), which leads me to believe that it is still serialized download... Also, function <em>retrieveFile()</em> returns, I quote documentation:</p>
  
  <blockquote>
    <p>True if successfully completetd, false if not</p>
  </blockquote>
  
  <p>What I need is <strong>serialized</strong> download, because I need to log every unsuccessful download.<br>
  What I saw browsing through the commons-net source is that, if I'm not wrong, new Socket is created for each <em>retrieveFile()</em> call.</p>
  
  <p>I'm pretty confused about this, so If someone could explain what is actually happening, and offer solution with this library, or recommend some other FTP java library that supports blocking download per file, that would be nice.</p>
  
  <p>Thanks.</p>