【问题标题】:Secure FTP with org.apache.commons.net.ftp.FTPClient使用 org.apache.commons.net.ftp.FTPClient 保护 FTP
【发布时间】:2012-09-30 00:21:52
【问题描述】:

有没有办法可以使用org.apache.commons.net.ftp.FTPClient 实现安全 FTP?

如果没有,Java 的其他选择是什么?

【问题讨论】:

    标签: java ftp sftp apache-commons ftp-client


    【解决方案1】:

    试用 Java 安全通道

    支持SFTP

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

    例子可以在here找到

    【讨论】:

      【解决方案2】:

      试试 Apache Camel 怎么样,

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

      【讨论】:

      • 告诉我们骆驼是什么
      【解决方案3】:

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

      【讨论】:

      【解决方案4】:

      您可以使用 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

      【讨论】:

        【解决方案5】:

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


        【讨论】:

        • 这是更合适的答案!对于 OP,除非他将 FTPS 误认为是安全 FTP,否则服务器似乎恰好支持 SFTP 和 FTPS。否则当前标记的正确答案将不起作用。
        猜你喜欢
        • 2012-09-25
        • 2013-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多