【问题标题】:Jsch example to copy file to SFTP Server将文件复制到 SFTP 服务器的 Jsch 示例
【发布时间】:2014-05-13 11:09:08
【问题描述】:

以下是我的程序...但这在 session.connect() 之后不起作用

public static void main(String args[])
        {
        try {
            String ftpHost = "XXXXXXXX";
            int ftpPort = 21;     
            String ftpUserName = "XXXX";
            String ftpPassword = "XXXXX";
            String ftpRemoteDirectory = "/";
            String fileToTransmit = "C://XXXXX//Desktop//RG//10171699_821972117859158_5724612734096298046_n.jpg";          
            JSch.setLogger(new MyLogger());
            System.out.println("Creating 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(ftpUserName, ftpHost, ftpPort);
                System.out.println("Session created.");
                session.setPassword(ftpPassword);

                java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
                System.out.println("Session connected before.");
                session.connect();
                System.out.println("Session connected.");

                System.out.println("OPEN SFTP CHANNEL");
                //
                // Open the SFTP channel
                //
                System.out.println("Opening Channel.");
                channel = session.openChannel("sftp");
                channel.connect();
                c = (ChannelSftp) channel;
                System.out.println("Now checing status");
            } catch (Exception e) {
                System.err.println("Unable to connect to FTP server."
                        + e.toString());
                throw e;
            }

            //
            // Change to the remote directory
            //
            System.out.println("Now performing operations");
            ftpRemoteDirectory="/home/pooja111/";
            System.out.println("Changing to FTP remote dir: "
                    + ftpRemoteDirectory);
            c.cd(ftpRemoteDirectory);

            //
            // Send the file we generated
            //
            try {
                File f = new File(fileToTransmit);
                System.out.println("Storing file as remote filename: "
                        + f.getName());
                c.put(new FileInputStream(f), f.getName());
            } catch (Exception e) {
                System.err
                        .println("Storing remote file failed." + e.toString());
                throw e;
            }   

            //
            // Disconnect from the FTP server
            //
            try {
                c.quit();
            } catch (Exception exc) {
                System.err.println("Unable to disconnect from FTPserver. "
                        + exc.toString());
            }

        } catch (Exception e) {
            System.err.println("Error: " + e.toString());
        }

        System.out.println("Process Complete.");
        System.exit(0);
    }

输出是

Creating session.
Session created.
Session connected before.
INFO: Connecting to ftp.olstr.com port 21
INFO: Connection established

我的代码控件在 session.connect() 行之后没有移动。

【问题讨论】:

  • 您尝试连接的服务器是ftpsftp?
  • 默认端口:ftp = 21, sftp = 22
  • 我正在使用 FTP 服务器。我的 SFTP 服务器端口是 3335 ..但是当我尝试时,我收到了正在创建会话。会话已创建。之前连接的会话。信息:连接到 ftp.olstr.com 端口 3335 无法连接到 FTP server.com.jcraft.jsch.JSchException:java.net.ConnectException:连接超时:连接错误:com.jcraft.jsch.JSchException:java.net .ConnectException:连接超时:连接过程完成。
  • 我认为您使用的是 ftps 而不是 sftp。 ftps 是基于 ssl 的 ftp,它与运行在 ssh 端口上的 sftp 完全不同。

标签: java sftp jsch


【解决方案1】:

有不同的端口用于不同的连接,例如 FTP、FTPS、SFTP、SSH 上的 FTP。使用适当的端口。这些是端口。 20 FTP 数据(文件传输协议),21 FTP(文件传输协议),22 SSH(安全外壳)。 Use session.connect(timeout) 。评论您的堆栈跟踪,以便我知道确切的错误是什么。尝试使用 22,看看错误是否仍然存在。

【讨论】:

  • 谢谢,我一直在检查我的 jscp 代码,使用端口 21,不知道 sftp 强制使用端口 22。谢谢!
  • 这不是强制要求。它的默认端口。你可以配置它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 2017-12-14
  • 1970-01-01
  • 2014-04-05
  • 2022-07-27
  • 1970-01-01
相关资源
最近更新 更多