【问题标题】:SFTP file transfer using Java JSch使用 Java JSch 进行 SFTP 文件传输
【发布时间】:2013-02-13 01:45:15
【问题描述】:

这是我的代码,它在远程服务器上检索文件的内容并显示为输出。

package sshexample;

import com.jcraft.jsch.*;
import java.io.*;

public class SSHexample 
{
public static void main(String[] args) 
{
    String user = "user";
    String password = "password";
    String host = "192.168.100.103";
    int port=22;

    String remoteFile="sample.txt";

    try
    {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        System.out.println("Establishing Connection...");
        session.connect();
        System.out.println("Connection established.");
        System.out.println("Creating SFTP Channel.");
        ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
        sftpChannel.connect();
        System.out.println("SFTP Channel created.");
        InputStream out= null;
        out= sftpChannel.get(remoteFile);
        BufferedReader br = new BufferedReader(new InputStreamReader(out));
        String line;
        while ((line = br.readLine()) != null) 
        {
            System.out.println(line);
        }
        br.close();
        sftpChannel.disconnect();
        session.disconnect();
    }
    catch(JSchException | SftpException | IOException e)
    {
        System.out.println(e);
    }
}
}

现在如何实现这个文件复制到本地主机的程序以及如何从本地主机复制文件到服务器。

这里如何使任何格式的文件的文件传输工作。

【问题讨论】:

    标签: java ssh sftp file-transfer jsch


    【解决方案1】:

    用法:

    sftp("file:/C:/home/file.txt", "ssh://user:pass@host/home");
    sftp("ssh://user:pass@host/home/file.txt", "file:/C:/home");
    

    Implementation

    【讨论】:

      【解决方案2】:

      使用 JSch 通过 SFTP 上传文件最简单的方法是:

      JSch jsch = new JSch();
      Session session = jsch.getSession(user, host);
      session.setPassword(password);
      session.connect();
      
      ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
      sftpChannel.connect();
      
      sftpChannel.put("C:/source/local/path/file.zip", "/target/remote/path/file.zip");
      

      类似的下载:

      sftpChannel.get("/source/remote/path/file.zip", "C:/target/local/path/file.zip");
      

      您可能需要deal with UnknownHostKey exception

      【讨论】:

        【解决方案3】:

        下面的代码对我有用

           public static void sftpsript(String filepath) {
        
         try {
          String user ="demouser"; // username for remote host
          String password ="demo123"; // password of the remote host
        
           String host = "demo.net"; // remote host address
          JSch jsch = new JSch();
          Session session = jsch.getSession(user, host);
          session.setPassword(password);
          session.connect();
        
          ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
          sftpChannel.connect();
        
          sftpChannel.put("I:/demo/myOutFile.txt", "/tmp/QA_Auto/myOutFile.zip");
          sftpChannel.disconnect();
          session.disconnect();
         }catch(Exception ex){
             ex.printStackTrace();
         }
           }
        

        或将 StrictHostKeyChecking 用作“否”(安全后果)

        public static void sftpsript(String filepath) {
        
         try {
          String user ="demouser"; // username for remote host
          String password ="demo123"; // password of the remote host
        
           String host = "demo.net"; // remote host address
          JSch jsch = new JSch();
           Session session = jsch.getSession(user, host, 22);
           Properties config = new Properties();
           config.put("StrictHostKeyChecking", "no");
           session.setConfig(config);;
           session.setPassword(password);
           System.out.println("user=="+user+"\n host=="+host);
           session.connect();
        
          ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
          sftpChannel.connect();
        
          sftpChannel.put("I:/demo/myOutFile.txt", "/tmp/QA_Auto/myOutFile.zip");
          sftpChannel.disconnect();
          session.disconnect();
         }catch(Exception ex){
             ex.printStackTrace();
         }
           }
        

        【讨论】:

        猜你喜欢
        • 2017-10-10
        • 1970-01-01
        • 2015-07-18
        • 1970-01-01
        • 2011-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多