使用org.apache.commons.net.ftp.FTPClient保护FTP

use*_*253 20 java ftp sftp ftp-client apache-commons

有没有办法实现安全的FTP org.apache.commons.net.ftp.FTPClient

如果没有,Java的其他选项有哪些?

Mar*_*ryl 35

首先,确保您了解FTPS(安全FTP)和SFTP:
FTPS与SFTP与SCP之间的区别


  • 这才是更合适的答案!对于OP来说,看起来服务器恰好支持SFTP和FTPS,除非他将FTPS误认为是安全FTP。否则当前标记的正确答案将不起作用。 (2认同)

小智 18

您可以使用org.apache.commons.net.ftp.FTPSClient而不是org.apache.commons.net.ftp.FTPClient具有安全ftp http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPSClient.html


Bab*_*emi 5

Apache FTPClient目前不支持SFTP。但是,您可以使用JSch-Java安全通道。

Onkar Joshi进一步介绍了该库的详细信息,该库可用于Java中的FTP,SFTP,FTPS文件传输。

使用JSch通过SFTP传输文件的示例如下:

    ...

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

}

...
Run Code Online (Sandbox Code Playgroud)

  • 产生FTPClient的Apache Commons Net项目确实支持SFTP。 (4认同)
  • @BabatundeAdeyemi 请不要建议任何人使用“StrictHostKeyChecking=no”而不解释其安全后果。有关正确的解决方案,请参阅[如何在使用 JSch SFTP 库时解析 Java UnknownHostKey?](/sf/ask/2299703451/) (2认同)