【问题标题】:Download SFTP file to Remote server folder directly (not to shared path)将 SFTP 文件直接下载到远程服务器文件夹(而不是共享路径)
【发布时间】:2017-04-29 20:43:03
【问题描述】:

我想开发一个 java 程序来将文件从 SFTP 服务器下载到远程服务器。该远程服务器没有任何共享路径。我必须直接从 sftp 下载并粘贴到远程 Windows 服务器驱动程序 (D:)。

代码:

 int ftpPort = 22;

    JSch jsch = new JSch  ();
         Session session = null;
         Channel channel = null;
         ChannelSftp c = null;

        try {

        session = jsch.getSession(username, hostname, ftpPort);
        logger.info("***   FTP Session created.   ***");
        session.setPassword(password);

        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 {
                logger.info("***   Storing file:'" + filename + "' to local directory: '"+localDirectory+"'");

我正在使用 Jsch 和 ChannelSftp 连接到 SFTP 服务器。

截至目前,以上代码将代码下载到本地路径和共享路径。

任何将文件下载到没有任何共享路径的远程服务器(Windows)的建议。

谢谢。

【问题讨论】:

    标签: java sftp jsch


    【解决方案1】:

    您的代码需要直接在远程服务器上运行并将文件从 SFTP 下载到其本地磁盘。

    如果您确实没有任何共享文件夹,另一种方法是使用您的代码在本地计算机上下载文件,然后使用 SCP 之类的东西在远程服务器上传输文件。

    scp /path/to/your/file user@host:/remote/path
    

    但你说,它是 Windows,所以你可能需要先在该机器上设置 SSH/SCP。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-11
      • 2021-11-20
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      相关资源
      最近更新 更多