FTPSClient文件上传和下载总是大小为0和异常

时间:2016-01-28 12:11:05

标签: ftp ftp-client filezilla ftps apache-commons-net

安装了filezilla服务器并启用了FTP over TLS Settings in Settings并启动了服务器。   通过eclipse java客户端我试图连接到服务器上传并使用下面的代码下载文件    使用commons-net apache库。

      FTPSClient ftpClient = new FTPSClient(false);     
      // Connect to host
      ftpClient.connect(mServer, mPort);
      int reply = ftpClient.getReplyCode();
      System.out.println("The reply code is "+reply);
      if (FTPReply.isPositiveCompletion(reply)) {

        // Login
        if (ftpClient.login("******", "*******")) {
          // Set protection buffer size
          ftpClient.execPBSZ(0);
          // Set data channel protection to private
          ftpClient.execPROT("P");
          // Enter local passive mode
          ftpClient.enterLocalPassiveMode();


            // Upload File using storeFile
            File firstLocalFile = new File("e:/Test.txt");
            String firstRemoteFile = "hello.txt";
            InputStream is = new FileInputStream(firstLocalFile);
            String result = getStringFromInputStream(is);
            System.out.println(result);

            Object output = ftpClient.storeFile(firstRemoteFile, is);
            System.out.println(output);
            is.close();

            // Download File using retrieveFile(String, OutputStream)
            String remoteFile1 = "/settings.xml";
            File downloadFile1 = new File("e:/testOutput.xml");
            OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
            boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
            outputStream1.close();

            if (success) {
                System.out.println("File #1 has been downloaded successfully.");
            }




      // Logout
      ftpClient.logout();

    // Disconnect
        ftpClient.disconnect();

        } else {
          System.out.println("FTP login failed");
        }

        // Disconnect
        ftpClient.disconnect();

      } else {
        System.out.println("FTP connect to host failed");
      }
    } catch (IOException ioe) {
      System.out.println("FTP client received network error");
      ioe.printStackTrace();
    } catch (Exception nsae) {
      System.out.println("FTP client could not use SSL algorithm");
      nsae.printStackTrace();
    }

它在服务器上创建一个文件hello.txt,但大小为0kb(源文件大小为10 kb),最终出现以下错误。请帮我解决这个问题

     javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at org.apache.commons.net.ftp.FTPSClient._openDataConnection_(FTPSClient.java:619)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:633)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)
at com.test.ftps.TestClass.main(TestClass.java:88)
  Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(Unknown Source)
... 9 more

2 个答案:

答案 0 :(得分:1)

只是取消勾选 “在Filezilla服务器中要求TLC会话恢复数据连接...” - >设置 - > FTP over TLS设置 - >在使用PROT P时,取消勾选要求TLC会话恢复数据连接

答案 1 :(得分:0)

除了user2750213的答案(Filezilla的TLS session resumption)之外,请注意启用所需的协议。您可以在连接到FTPS服务器的jvm上验证它们是运行this code还是this other。最新版本的Filezilla服务器使用TLSv1.2。

如果这对你有用,你可能会得到 java.net.SocketException:未实现的未连接套接字。在这种情况下,您需要编写自己的类,扩展DefaultSocketFactory类,然后通过方法ftpsClient.setSocketFactory(yourSocketFactory)覆盖createSocket()方法将其设置为FTPS客户端,该方法必须返回一个新的Socket()