Apache commons-net FTP错误

jsp*_*dnl 0 java ftp apache-commons apache-commons-net

我试图在java中实现ftp下载.我正在使用apache common-net library但是我得到了这个异常.我打印出下面的堆栈跟踪我不知道我错过了什么

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)
Run Code Online (Sandbox Code Playgroud)

我有一个方法如下

  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();
  }
Run Code Online (Sandbox Code Playgroud)

请帮助我错过的东西.

jsp*_*dnl 5

抱歉! 因为我的愚蠢

  //login to server
if (!ftpClient.login("username", "password")) {
  ftpClient.logout();
}
Run Code Online (Sandbox Code Playgroud)

这是错误,正在两次注销