title: JAVA 6 and corrupted files after uploading using Apache FTPClient 3.1 and FTP.BINARY_FILE_TYPE
body: <p>I'm really going bonkers on this.</p>
  
  <p>I'm writing a small Java (must be SDK 6) tool using the commons-net-3.1.jar library to upload a bunch of files to another bunch of servers simultaneously. I will use it with .jpg images mostly. I set the connection to BINARY_FILE_TYPE before anyone ask :)</p>
  
  <p>When I try and run it, everything goes smooth but sometimes, after uploading the images, they have "small lines" on them, like if the file had been corrupted on its way.</p>
  
  <p>This little piece of code is the one who does all the work. Any clues about how should I manage the first catches when the os.write fails in order to keep the file uploading right?</p>
  
  <pre><code>try {
      InputStream is = new FileInputStream(rutaFichero);
      OutputStream os = ftp.storeFileStream(nombreFichero);
      byte buf[] = new byte[8192];
      bytesRead = is.read(buf);
  
      while ((bytesRead = is.read(buf)) != -1) {
          try {
              os.write(buf, 0, bytesRead);
          } catch (IOException ioe) {
          } catch (NullPointerException npe) {
          }
      }
  
      is.close();
      try {
          os.close();
          completado = ftp.completePendingCommand();
          if (completado) {
      } catch (NullPointerException npe) {}
  }
  </code></pre>
  
  <p>Out of curiosity, I thought it could be just bad conditions on the line so I checked the sizes of files after transfering for some days and... none of them matched! WTF!?</p>