简体   繁体   English

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

[英]Secure FTP with org.apache.commons.net.ftp.FTPClient

Is there a way I can implement a secure FTP with org.apache.commons.net.ftp.FTPClient ? 有没有办法用org.apache.commons.net.ftp.FTPClient实现安全的FTP?

If not, what are other options for Java? 如果没有,Java的其他选项有哪些?

First, make sure you understand a difference between FTPS (Secure FTP) and SFTP: 首先,确保您了解FTPS(安全FTP)和SFTP之间的区别:
FTPS versus SFTP versus SCP FTPS与SFTP对比SCP


You can use org.apache.commons.net.ftp. 您可以使用org.apache.commons.net.ftp。 FTPSClient instead of org.apache.commons.net.ftp. FTPSClient而不是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 FTPClient具有安全ftp http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPSClient.html

Try Java Secure Channel 尝试Java安全通道

It supports SFTP 它支持SFTP

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

Example can be found here 示例可以在这里找到

Apache FTPClient doesn't support SFTP at the moment. Apache FTPClient目前不支持SFTP。 However, you can use JSch - Java Secure Channel. 但是,您可以使用JSch - Java安全通道。

Onkar Joshi goes into more details of the library to use for FTP, SFTP, FTPS file transfer in Java. Onkar Joshi详细介绍了用于FTP,SFTP,Java中FTPS文件传输的库。

An example of using JSch to transfer files with SFTP is as follows: 使用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.   ***");
    }

}

...

How about trying Apache Camel, 怎么样尝试Apache Camel,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用“ org.apache.commons.net.ftp.FTPClient”解决异常 - How to solve exception with “org.apache.commons.net.ftp.FTPClient” org.apache.commons.net.ftp.FTPClient listFiles()的问题 - Issue with org.apache.commons.net.ftp.FTPClient listFiles() 如何导入 org.apache.commons.net.ftp.FTPClient - How to import org.apache.commons.net.ftp.FTPClient FTPClient(org.apache.commons.net.ftp.FTPClient)无法检索大型xml文件 - FTPClient(org.apache.commons.net.ftp.FTPClient) is not able to retrieve large xml file ftp客户端上的noClassDefFound错误:org.apache.commons.net.ftp.FTPClient - noClassDefFound error on ftp client: org.apache.commons.net.ftp.FTPClient org.apache.commons.net.ftp.FTPClient 的问题,获取 FTP 响应 421 接收错误 - Problems with org.apache.commons.net.ftp.FTPClient, getting FTP response 421 received error 使用org.apache.commons.net.ftp.FTPClient从AsyncTask类登录FTP时出错 - Error when logging into FTP from AsyncTask class using org.apache.commons.net.ftp.FTPClient NoRouteToHostException尝试以SFTP模式通过org.apache.commons.net.ftp.FTPClient连接到远程主机 - NoRouteToHostException trying to connect to remote host via org.apache.commons.net.ftp.FTPClient in SFTP mode 致命异常:main java.lang.NoClassDefFoundError:org.apache.commons.net.ftp.FTPClient Android Studio - FATAL EXCEPTION: main java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient Android Studio org.apache.commons.net.ftp.FTPClient listFiles 总是从根目录返回文件,而与参数中给出的路径名无关 - org.apache.commons.net.ftp.FTPClient listFiles always returns files from root directory irrespective of the pathname given in argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM