【问题标题】:Apache commons-net FTP ErrorApache commons-net FTP 错误
【发布时间】:2015-10-30 16:39:05
【问题描述】:

我正在尝试在 java 中实现 ftp 下载。我正在使用 apache common-net 库,但我得到了这个异常。我打印了下面的堆栈跟踪我不确定我错过了什么

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)

我有一个方法如下

  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();
  }

请帮助我错过了什么。

【问题讨论】:

    标签: java ftp apache-commons apache-commons-net


    【解决方案1】:

    对不起!因为我的愚蠢

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

    这里是错误,正在注销两次

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-17
      • 2012-01-25
      • 2011-10-02
      • 2018-07-07
      • 2011-01-08
      • 1970-01-01
      • 2010-11-09
      • 2015-01-24
      相关资源
      最近更新 更多