Sécuriser FTP avec org. apache. commons. net. ftp. FTPClient


Existe-t-il un moyen d'implémenter un FTP sécurisé avec org.apache.commons.net.ftp.FTPClient?

Sinon, quelles sont les autres options pour Java?

Author: Martin Prikryl, 2012-10-09

5 answers

Vous pouvez utiliser org. apache. commons. net. ftp. FTPSClient au lieu de org. apache. commons. net. ftp. FTPClient pour avoir un ftp sécurisé http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPSClient.html

 22
Author: user1464713, 2014-04-01 20:19:29

Tout d'abord, assurez-vous de comprendre une différence entre FTPS (FTP sécurisé) et SFTP:
FTPS versus SFTP versus SCP


 45
Author: Martin Prikryl, 2019-09-18 11:01:50

Essayez le canal sécurisé Java

Il prend en charge SFTP

Http://www.jcraft.com/jsch/

Par Exemple ici

 4
Author: crunchBar, 2012-10-09 16:20:41

Apache FTPClient ne prend pas en charge SFTP pour le moment. Cependant, vous pouvez utiliser JSch-Java Secure Channel.

Onkar Joshi donne plus de détails sur la bibliothèque à utiliser pour le transfert de fichiers FTP, SFTP, FTPS en Java.

Un exemple d'utilisation de JSch pour transférer des fichiers avec SFTP est le suivant:

    ...

private static final Logger logger = Logger.getLogger(YourClass.class.getName());

public boolean sendDataViaSFTP(String contents) throws Exception {
    String hostname = "<FTP hostname/IP>";
    String username = "<FTP username>";
    String password = "<FTP password>";
    String remoteDirectory = "<FTP remote directory>";
    int ftpPort = 22;

    logger.info("***   Creating FTP session.   ***");
    JSch jsch = new JSch();
    Session session = null;
    Channel channel = null;
    ChannelSftp c = null;

    //Now connect and SFTP to the SFTP Server
    try {
        //Create a session sending through our username and password
        session = jsch.getSession(username, hostname, ftpPort);
        logger.info("***   FTP Session created.   ***");
        session.setPassword(password);

        //Security.addProvider(new com.sun.crypto.provider.SunJCE());
        //Setup Strict HostKeyChecking to no so we dont get the
        //unknown host key exception
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        logger.info("***   Session connected.   ***");

        //Open the SFTP channel
        logger.info("***   Opening FTP Channel.   ***");
        channel = session.openChannel("sftp");
        channel.connect();
        c = (ChannelSftp) channel;

        //Change to the remote directory
        logger.info("***   Changing to FTP remote dir: " + remoteDirectory + "   ***");
        c.cd(remoteDirectory);

        //Send the file we generated
        try {
            String filename = "myfile.txt";

            logger.info("***   Storing file as remote filename: " + filename + "   ***");

            ByteArrayInputStream bis = new ByteArrayInputStream(contents.getBytes());
            c.put(bis, filename);
            return true;
        } catch (Exception e) {
            logger.info("***   Storing remote file failed. " + e.toString() + "   ***");
            throw e;
        }
    } catch (Exception e) {
        logger.info("***   Unable to connect to FTP server. " + e.toString() + "   ***");
        throw e;
    } finally {
        //
        //Disconnect from the FTP server
        //
        try {
            if(session != null)
                session.disconnect();

            if(channel != null)
                channel.disconnect();

            if(c != null)
                c.quit();
        } catch (Exception exc) {
            logger.severe("***   Unable to disconnect from FTP server. " + exc.toString()+"   ***");
        }

        logger.info("***   SFTP Process Complete.   ***");
    }

}

...
 4
Author: Babatunde Adeyemi, 2014-02-28 14:47:03

Que diriez-vous d'essayer Apache Camel,

Http://camel.apache.org/ftp2.html

 2
Author: ymnk, 2012-10-24 02:43:11