25

Is there a way I can implement a secure FTP with org.apache.commons.net.ftp.FTPClient?

If not, what are other options for Java?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user1725253
  • 329
  • 1
  • 4
  • 13

5 Answers5

51

First, make sure you understand a difference between FTPS (Secure FTP) and SFTP:
FTPS versus SFTP versus SCP


Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 2
    This is the more appropriate answer! For OP it looks like SFTP and FTPS happen to be supported by the server unless he mistook FTPS as Secure FTP. Otherwise the currently marked correct answer would not have worked. – Sai Aug 13 '19 at 20:23
24

You can use org.apache.commons.net.ftp.FTPSClient instead of org.apache.commons.net.ftp.FTPClient to have secure ftp http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPSClient.html

Alaa Zayed
  • 304
  • 2
  • 4
4

Try Java Secure Channel

It supports SFTP

http://www.jcraft.com/jsch/

Example can be found here

crunchBar
  • 111
  • 4
4

Apache FTPClient doesn't support SFTP at the moment. However, you can use JSch - Java Secure Channel.

Onkar Joshi goes into more details of the library to use for FTP, SFTP, FTPS file transfer in Java.

An example of using JSch to transfer files with SFTP is as follows:

    ...

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.   ***");
    }

}

...
Babatunde Adeyemi
  • 14,360
  • 4
  • 34
  • 26
  • There's a good reason for it too; SFTP is something entirely different from FTP. Its pretty much a 'Java is not Javascript' deal :) – Gimby Feb 28 '14 at 15:13
  • 4
    The Apache Commons Net project that produces FTPClient DOES support SFTP though. – BrianC Mar 17 '14 at 18:59
  • 1
    @BrianC Imo, Apache Commons Net project does not support SFTP. There's Apache project for SFTP though, if that's you mean – [Apache MINA](https://mina.apache.org/mina-project/) – See [How to write SFTP client using Apache MINA library](https://stackoverflow.com/q/55137545/850848) – Or [Apache Camel](https://camel.apache.org/components/latest/sftp-component.html), which ironically uses JSch. – Martin Prikryl Sep 18 '19 at 11:08
  • 2
    @BabatundeAdeyemi Please do not suggest anyone to use `StrictHostKeyChecking=no` without explaining its security consequences. For the correct solution, see [How to resolve Java UnknownHostKey, while using JSch SFTP library?](https://stackoverflow.com/q/32852906/850848) – Martin Prikryl Sep 18 '19 at 11:11
2

How about trying Apache Camel,

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

ymnk
  • 1,145
  • 7
  • 7