连接linux系统sftp下载文件

1.去官网下载最新的jar包 
jsch-0.1.51.jar 

2.运行官方例子 
Shell.java 
演示了如何连接到sshd服务器 
Exec.java 
演示了登陆上去并执行一条命令 

注意以上2个例子让你输入密码同样也是输入cyg_server用户的密码 
运行成功后可以看下它的代码。 

3.再做一个SFTP试验 

Java代码  收藏代码
  1. import java.io.*;  
  2. import java.util.*;  
  3. import com.jcraft.jsch.*;  
  4.   
  5. public class Test1 {  
  6.       
  7.     public static void main(String[] args) throws Exception {  
  8.         Test1.sshSftp("127.0.0.1""Administrator""cyg_server"22);  
  9.         Test1.sshSftp2("127.0.0.1""Administrator"22"C:/Users/Administrator/rsa_my""");  
  10.     }  
  11.       
  12.     /** 
  13.      * 利用JSch包实现SFTP下载、上传文件(用户名密码方式登陆) 
  14.      * @param ip 主机IP 
  15.      * @param user 主机登陆用户名 
  16.      * @param psw  主机登陆密码 
  17.      * @param port 主机ssh2登陆端口,如果取默认值(默认值22),传-1 
  18.      *  
  19.      */  
  20.     public static void sshSftp(String ip, String user, String psw   
  21.             ,int port) throws Exception{  
  22.         System.out.println("开始用户名密码方式登陆");  
  23.         Session session = null;  
  24.           
  25.         JSch jsch = new JSch();  
  26.           
  27.         if(port <=0){  
  28.             //连接服务器,采用默认端口  
  29.             session = jsch.getSession(user, ip);  
  30.         }else{  
  31.             //采用指定的端口连接服务器  
  32.             session = jsch.getSession(user, ip ,port);  
  33.         }  
  34.   
  35.         //如果服务器连接不上,则抛出异常  
  36.         if (session == null) {  
  37.             throw new Exception("session is null");  
  38.         }  
  39.           
  40.         //设置登陆主机的密码  
  41.         session.setPassword(psw);//设置密码     
  42.         //设置第一次登陆的时候提示,可选值:(ask | yes | no)  
  43.         session.setConfig("StrictHostKeyChecking""no");  
  44.         //设置登陆超时时间     
  45.         session.connect(30000);  
  46.           
  47.         sftp(session, "aa.log");  
  48.         System.out.println("sftp成功");  
  49.     }  
  50.       
  51.     /** 
  52.      * 利用JSch包实现SFTP下载、上传文件(秘钥方式登陆) 
  53.      * @param ip 主机IP 
  54.      * @param user 主机登陆用户名 
  55.      * @param port 主机ssh2登陆端口,如果取默认值(默认值22),传-1 
  56.      * @param privateKey 密钥文件路径 
  57.      * @param passphrase 密钥的密码 
  58.      *  
  59.      */  
  60.     public static void sshSftp2(String ip, String user  
  61.             ,int port ,String privateKey ,String passphrase) throws Exception{  
  62.         System.out.println("开始秘钥方式登陆");  
  63.         Session session = null;  
  64.           
  65.         JSch jsch = new JSch();  
  66.           
  67.         //设置密钥和密码  
  68.         //支持密钥的方式登陆,只需在jsch.getSession之前设置一下密钥的相关信息就可以了  
  69.         if (privateKey != null && !"".equals(privateKey)) {  
  70.                if (passphrase != null && "".equals(passphrase)) {  
  71.                 //设置带口令的密钥  
  72.                    jsch.addIdentity(privateKey, passphrase);  
  73.                } else {  
  74.                 //设置不带口令的密钥  
  75.                    jsch.addIdentity(privateKey);  
  76.                }  
  77.         }  
  78.           
  79.           
  80.         if(port <=0){  
  81.             //连接服务器,采用默认端口  
  82.             session = jsch.getSession(user, ip);  
  83.         }else{  
  84.             //采用指定的端口连接服务器  
  85.             session = jsch.getSession(user, ip ,port);  
  86.         }  
  87.   
  88.         //如果服务器连接不上,则抛出异常  
  89.         if (session == null) {  
  90.             throw new Exception("session is null");  
  91.         }  
  92.           
  93.         //设置第一次登陆的时候提示,可选值:(ask | yes | no)  
  94.         session.setConfig("StrictHostKeyChecking""no");  
  95.         //设置登陆超时时间  
  96.         session.connect(30000);  
  97.           
  98.         sftp(session, "bb.log");  
  99.         System.out.println("sftp成功");  
  100.     }  
  101.       
  102.     private static void sftp(Session session, String uploadFileName) throws Exception {  
  103.         Channel channel = null;  
  104.         try {  
  105.             //创建sftp通信通道  
  106.             channel = (Channel) session.openChannel("sftp");  
  107.             channel.connect(1000);  
  108.             ChannelSftp sftp = (ChannelSftp) channel;  
  109.               
  110.               
  111.             //进入服务器指定的文件夹  
  112.             sftp.cd("testsftp");  
  113.               
  114.             //列出服务器指定的文件列表  
  115.             Vector v = sftp.ls("*.txt");  
  116.             for(int i=0;i<v.size();i++){  
  117.                 System.out.println(v.get(i));  
  118.             }  
  119.               
  120.             //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了  
  121.             OutputStream outstream = sftp.put(uploadFileName);  
  122.             InputStream instream = new FileInputStream(new File("C:/ftptest/aa.txt"));  
  123.               
  124.             byte b[] = new byte[1024];  
  125.             int n;  
  126.             while ((n = instream.read(b)) != -1) {  
  127.                 outstream.write(b, 0, n);  
  128.             }  
  129.               
  130.             outstream.flush();  
  131.             outstream.close();  
  132.             instream.close();  
  133.         } catch (Exception e) {  
  134.             e.printStackTrace();  
  135.         } finally {  
  136.             session.disconnect();  
  137.             channel.disconnect();  
  138.         }  
  139.     }  
  140. }  


代码是引用了http://my.oschina.net/hetiangui/blog/137357 
首先这句sftp.ls("*.txt")是列出指定文件夹下所有txt文件, 
然后sftp.put("3.txt")是上传一个文件到SFTP服务器上。 

注意main函数里有2种登陆方式, 
第一种是通过用户名Administrator,密码cyg_server的方式登陆。 
第二种是通过秘钥方式登陆,我们的私钥目录是"C:/Users/Administrator/rsa_my",而passphrase则为空。 

原文地址:https://www.cnblogs.com/hzcya1995/p/13318011.html