Uploaded image for project: 'Commons Net'
  1. Commons Net
  2. NET-12

[net] FTPClient.retrieveFile() results in 0 byte files

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Incomplete
    • 1.4
    • None
    • None
    • None
    • Operating System: HP-UX
      Platform: HP

    • 35260

    Description

      FTPClient's retrieveFile() always returns false and results in 0 byte files
      FTPClient's completePendingCommand() always never returns when uncommented out
      Attempted client on Win2000 and Server on HPUX 11, also client and server
      both on HPUX.

      /lib contains commons-net-1.4.0.jar and jakarta-oro-2.0.8.jar.

      Thanks,
      Steve

      Relative Source Code:

      import org.apache.commons.net.ftp.*;

      import java.io.BufferedInputStream;
      import java.io.BufferedOutputStream;
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.PrintWriter;
      import java.rmi.dgc.VMID;
      import java.text.DateFormat;
      import java.util.Calendar;
      import java.util.GregorianCalendar;
      import java.util.Date;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipOutputStream;

      private void get(Calendar criteriaDateTime) throws Exception {

      FTPClient ftp = null;
      FTPFile[] serverFiles = null;
      boolean isLoggedIn = false;
      Exception exception = null;

      Date serverFileDate;

      try {
      // Connect and logon to FTP Server
      ftp = new FTPClient();
      ftp.connect(server);
      isLoggedIn = ftp.login(userId, password);
      if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))

      { throw new Exception("FTP server refused connection."); }

      Logger.log("ftp post-login reply: " + ftp.getReplyString
      ());

      // Use passive mode assuming we are behind a firewall.
      ftp.enterLocalPassiveMode();
      // default to binary transfer
      ftp.setFileType(FTP.BINARY_FILE_TYPE);

      Logger.log("ftp status: " + ftp.getStatus());

      // get list of files in FTP server directory
      FTPListParseEngine engine = ftp.initiateListParsing
      (serverDirectory);

      if(engine.hasNext()) {
      serverFiles = engine.getFiles();
      Logger.log("Number of files in server
      directory: " + serverFiles.length);

      // what if the file is removed by another
      process?

      for (int i = 0; i < serverFiles.length; i++) {

      if(serverFiles[i].isFile()){
      serverFileDate = serverFiles
      [i].getTimestamp().getTime();

      // select files form the server
      if file attributes have not changed in x seconds
      if (serverFileDate.compareTo
      (criteriaDateTime.getTime()) <= 0) {
      if(isZipClientFile)

      { doFtpZip(ftp, serverFiles[i]); }

      else

      { doFtpOnly(ftp, serverFiles[i]); }

      // delete server file
      if requested
      if(isDeleteServerFile)

      { ftp.deleteFile (serverFiles[i].getName()); }

      }
      }
      }
      }

      } catch (Exception e)

      { Logger.handleError(this.getClass().getName(), "get", e); exception = e; }

      finally{
      // Logout and disconnect from the FTP Server
      if(ftp != null)

      { if(isLoggedIn) ftp.logout(); if(ftp.isConnected()) ftp.disconnect(); }

      }
      if(exception != null)

      { throw exception; }
      }

      private void doFtpOnly(FTPClient ftp, FTPFile serverFile) throws
      Exception {

      DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
      int count;
      byte data[] = new byte[BUFFER];
      FileOutputStream fos = null;
      Exception exception = null;

      try{
      Logger.log("FTP Download: " +
      serverFile.getName() +
      " with timestamp " +
      df.format(serverFile.getTimestamp
      ().getTime()));

      fos = new FileOutputStream(
      clientDirectory +
      File.separator +
      serverFile.getName());

      // stream the ftp contents into an inputstream
      // this always returns false
      if(!ftp.retrieveFile(serverFile.getName(), fos)){ // throw new Exception("false return from ftp.retrieveFile()"); Logger.log("false return from ftp.retrieveFile ()"); }
      /* this command freezes every time when running on
      Windows */
      Logger.log("to ftp.completePendingCommand()");
      if(!ftp.completePendingCommand()){ throw new Exception("completePendingCommand() returned false"); }
      Logger.log("did ftp.completePendingCommand()");
      }
      catch(Exception e){ Logger.handleError(this.getClass().getName(), "doFtpOnly", e); exception = e; }
      finally{ // close input stream if(fos != null) fos.close(); Logger.log("FTP end"); }
      if(exception != null){ throw exception; }

      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            steven.levander@infores.com Steven LeVander
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: