在FTPS中获取SSL握手异常

时间:2017-04-12 09:10:24

标签: java ssl-certificate apache-commons-net

package routines;


import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;



import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;

import javax.net.ssl.KeyManager;

import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.util.KeyManagerUtils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

@SuppressWarnings("deprecation")
public class FTPSTest {



    public static void main() throws GeneralSecurityException {
        getFile("*******",990,"***","**********","***************","*.csv");
    }


      public static void getFile(String host,
                          int port,
                          String username,
                          String password,
                          String localDirectory,
                          String remoteDirectory,
                          String filemask) throws GeneralSecurityException {
        try {
//          FTPSClient.setTrustManager(null);
          SSLContext context = SSLContext.getInstance("TLSv1.2");
          context.init(null, null, new java.security.SecureRandom()); 
          FTPSClient ftpClient = new FTPSClient(true,context);
          // Connect to host
          ftpClient.setTrustManager(null);
          ftpClient.setAuthValue("SSL");
          //ftpClient.setTrustManager(getConfiguration().getCertificatesManager());
          KeyManager keyManager = KeyManagerUtils.createClientKeyManager(new File("/home/global/Desktop/wilson.jks"), "W1nd3n3rgy17!");
          ftpClient.setKeyManager(keyManager);
          ftpClient.connect(host, port);
          int reply = ftpClient.getReplyCode();
          System.out.println(reply);
          if (FTPReply.isPositiveCompletion(reply)) {

            // Login
            if (ftpClient.login(username, password)) {

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

              System.out.println("connected");

代码成功运行直到此步骤,但是当它执行下一行时,它会抛出异常

              //Get file from host
              for(FTPFile ftpfiles: ftpClient.listDirectories()){
                  System.out.println(">>>"+ftpfiles.getName());
              }
              ftpClient.changeWorkingDirectory(remoteDirectory);
              for(FTPFile ftpfiles: ftpClient.listDirectories()){
                  System.out.println("<<<"+ftpfiles.getName());
              }
              System.out.println("connection established");
              ftpClient.setTrustManager(null);
              FTPFile[] ftpfiles= ftpClient.listFiles(); 
              System.out.println(ftpfiles.length);
              if (ftpfiles != null && ftpfiles.length > 0){
                  for (FTPFile file : ftpfiles) {
                     System.out.println(file.getName());
                     String fileName = file.getName();
                      if (fileName.matches(filemask)){
                        InputStream initialStream=ftpClient.retrieveFileStream(remoteDirectory+File.separatorChar+fileName);
                          byte[] buffer = new byte[initialStream.available()];
                          initialStream.read(buffer);
                         File targetFile = new File(localDirectory+File.separatorChar+fileName);
                         OutputStream outStream = new FileOutputStream(targetFile);
                         outStream.write(buffer);
                         outStream.close();

                      }
                  }
              }




          // Logout
          ftpClient.logout();

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

            // Disconnect
            ftpClient.disconnect();

          } else {
            System.out.println("FTP connect to host failed");
          }
        } catch (IOException ioe) {
            ioe.printStackTrace();
          System.out.println("FTP client received network error");
        } //catch (NoSuchAlgorithmException nsae) {
         // System.out.println("FTP client could not use SSL algorithm");
        //}
 //catch (GeneralSecurityException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        //}
      }


    private static Object getConfiguration() {
        // TODO Auto-generated method stub
        return null;
    }
}

/ *** *******输出/

   220
connected
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
  at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:992)
  at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
  at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)

FTP client received network error
[statistics] disconnected
  at analytics.etl_load_ftp_0_1.ETL_LOAD_FTP.tJava_4Process(ETL_LOAD_FTP.java:1740)
  at analytics.etl_load_ftp_0_1.ETL_LOAD_FTP.tJava_2Process(ETL_LOAD_FTP.java:1656)
  at analytics.etl_load_ftp_0_1.ETL_LOAD_FTP.tLibraryLoad_1Process(ETL_LOAD_FTP.java:1549)
  at analytics.etl_load_ftp_0_1.ETL_LOAD_FTP.runJobInTOS(ETL_LOAD_FTP.java:2740)
  at analytics.etl_load_ftp_0_1.ETL_LOAD_FTP.main(ETL_LOAD_FTP.java:1849)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
  at sun.security.ssl.InputRecord.read(InputRecord.java:505)
  at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
  ... 17 more

它能够成功地连接但是在获取文件的同时它抛出异常但不知道为什么??

我还设置了How to solve javax.net.ssl.SSLHandshakeException Error?

建议的keymanager
 KeyManager keyManager = KeyManagerUtils.createClientKeyManager(new File("/home/global/Desktop/wilson.jks"), "W1nd3n3rgy17!");

我也设置了TLSV1.2

0 个答案:

没有答案