com.logic.test.FTPSLogic.java Source code

Java tutorial

Introduction

Here is the source code for com.logic.test.FTPSLogic.java

Source

package com.logic.test;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPSClient;

/**
 *
 * @author josh
 */
public class FTPSLogic {
    public static void main(String[] args) {
        String serverAdress = "62.2.176.167";
        String username = "RLSFTPRead";
        String password = "ftp4rls";
        int port = 990;
        FTPSClient ftpsClient = new FTPSClient("TLS", true);
        String remoteFile = "REM - Persons Extract.csv";
        File localFile = new File("Persons Extract.csv");

        try {
            TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            } };

            ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

            //ftpsClient.setTrustManager(trustManager[0]);
            //KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            //kmf.init(null, null);
            //KeyManager km = kmf.getKeyManagers()[0];

            //ftpsClient.setKeyManager(km);
            ftpsClient.setBufferSize(1024 * 1024);
            ftpsClient.setConnectTimeout(100000);
            ftpsClient.connect(InetAddress.getByName(serverAdress), port);
            ftpsClient.setSoTimeout(100000);

            if (ftpsClient.login(username, password)) {
                ftpsClient.execPBSZ(0);
                ftpsClient.execPROT("P");
                ftpsClient.changeWorkingDirectory("/");
                ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpsClient.enterLocalPassiveMode();

                //ftpsClient.retrieveFile(remoteFile, new FileOutputStream(localFile));
                for (FTPFile file : ftpsClient.listFiles()) {
                    System.out.println("Nom " + file.getName());
                }

            }
        } catch (SocketException e) {
            ;
        } catch (UnknownHostException e) {
            ;
        } catch (IOException e) {
            ;
        } catch (Exception e) {
            ;
        } finally {
            try {
                ftpsClient.logout();
            } catch (Exception e2) {
            }

            try {
                ftpsClient.disconnect();
            } catch (Exception e2) {
            }
        }
    }
}