有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java直接将SFTP文件下载到远程服务器文件夹(而不是共享路径)

我想开发一个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)

谢谢


共 (1) 个答案

  1. # 1 楼答案

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

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

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

    但是你说,这是Windows,所以你可能需要先在那台机器上设置SSH/SCP