jsphdnl
jsphdnl

Reputation: 425

Apache commons-net FTP Error

I am trying to implement ftp download in java. I am using apache common-net library but I am getting this exception.I have the below stack trace printed out I not sure what am I missing

org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:317)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:608)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:582)
at org.apache.commons.net.ftp.FTP.pasv(FTP.java:1007)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:869)
at org.apache.commons.net.ftp.FTPClient._retrieveFile(FTPClient.java:1854)
at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1845)

I have a the method as follows

  public void downloadFromFtp(Map<String, String> ftpMap, String sourceWithPath,
      String destinationFolder) throws IOException {

    String hostname = ftpMap.get("hostname");
    String username = ftpMap.get("username");
    String password = ftpMap.get("password");

    if (null == hostname || null == username || null == password) {
      throw new InvalidInputException(
          "Invalid RMS FTP hostname/username/password");
    }
    //Connect to ftp url
    ftpClient.connect(hostname, 21);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

    //login to server
    if (!ftpClient.login("username", "password")) {
      ftpClient.logout();
    }
    int reply = ftpClient.getReplyCode();
    //FTPReply stores a set of constants for FTP reply codes.
    if (!FTPReply.isPositiveCompletion(reply)) {
      ftpClient.disconnect();
    }

    //enter passive mode
    ftpClient.enterLocalPassiveMode();
    File tempFile = new File(sourceWithPath);

    OutputStream output =
        new BufferedOutputStream(new FileOutputStream(destinationFolder + "/" + tempFile.getName()));

    ftpClient.retrieveFile(sourceWithPath, output);

    output.close();

    ftpClient.logout();
    ftpClient.disconnect();
  }

Please Help what Iam I missing.

Upvotes: 0

Views: 5658

Answers (1)

jsphdnl
jsphdnl

Reputation: 425

Sorry! for my stupidity

  //login to server
if (!ftpClient.login("username", "password")) {
  ftpClient.logout();
}

Here is the mistake, was doing logout twice

Upvotes: 5

Related Questions