JSch:纯JAVA实现SFTP文件上传和下载

来源:互联网 发布:映射端口怎么设置 编辑:程序博客网 时间:2024/05/17 08:04

转自:http://my.oschina.net/hetiangui/blog/137357

如果你想用纯JAVA实现SFTP文件上传或下载,或者是想纯JAVA连接到SSH2服务器上执行命令,那就使用JSch.jar包吧。这里我们先描述实现SFTP协议的上传和下载,上代码和详细的代码注释:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * 利用JSch包实现SFTP下载、上传文件
 * @param ip 主机IP
 * @param user 主机登陆用户名
 * @param psw  主机登陆密码
 * @param port 主机ssh2登陆端口,如果取默认值,传-1
 */
publicstatic void sshSftp(String ip, String user, String psw ,intport) throwsException{
    Session session = null;
    Channel channel = null;
 
     
    JSch jsch = newJSch();
     
     
    if(port <=0){
        //连接服务器,采用默认端口
        session = jsch.getSession(user, ip);
    }else{
        //采用指定的端口连接服务器
        session = jsch.getSession(user, ip ,port);
    }
 
    //如果服务器连接不上,则抛出异常
    if(session == null) {
        thrownew Exception("session is null");
    }
     
    //设置登陆主机的密码
    session.setPassword(psw);//设置密码  
    //设置第一次登陆的时候提示,可选值:(ask | yes | no)
    session.setConfig("StrictHostKeyChecking","no");
    //设置登陆超时时间  
    session.connect(30000);
         
    try{
        //创建sftp通信通道
        channel = (Channel) session.openChannel("sftp");
        channel.connect(1000);
        ChannelSftp sftp = (ChannelSftp) channel;
         
         
        //进入服务器指定的文件夹
        sftp.cd("domains");
         
        //列出服务器指定的文件列表
        Vector v = sftp.ls("*.txt");
        for(inti=0;i<v.size();i++){
            System.out.println(v.get(i));
        }
         
        //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
        OutputStream outstream = sftp.put("1.txt");
        InputStream instream = newFileInputStream(newFile("c:/print.txt"));
         
        byteb[] = newbyte[1024];
        intn;
        while((n = instream.read(b)) != -1) {
            outstream.write(b,0, n);
        }
         
        outstream.flush();
        outstream.close();
        instream.close();
    }catch(Exception e) {
        e.printStackTrace();
    }finally{
        session.disconnect();
        channel.disconnect();
    }
}


另外,JSCH也支持密钥的方式登陆,只需在jsch.getSession之前设置一下密钥的相关信息就可以了,以下是带密钥的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
 * 利用JSch包实现SFTP下载、上传文件
 * @param ip 主机IP
 * @param user 主机登陆用户名
 * @param psw  主机登陆密码
 * @param port 主机ssh2登陆端口,如果取默认值(默认值22),传-1
 * @param privateKey 密钥文件路径
 * @param passphrase 密钥的密码
 *
 */
publicstatic void sshSftp(String ip, String user, String psw
        ,intport ,String privateKey ,String passphrase) throwsException{
    Session session = null;
    Channel channel = null;
 
     
    JSch jsch = newJSch();
     
    //设置密钥和密码
    if(privateKey != null&& !"".equals(privateKey)) {
        if(passphrase != null&& "".equals(passphrase)) {
            //设置带口令的密钥
            jsch.addIdentity(privateKey, passphrase);
        }else{
            //设置不带口令的密钥
            jsch.addIdentity(privateKey);
        }
    }
 
     
     
    if(port <=0){
        //连接服务器,采用默认端口
        session = jsch.getSession(user, ip);
    }else{
        //采用指定的端口连接服务器
        session = jsch.getSession(user, ip ,port);
    }
 
    //如果服务器连接不上,则抛出异常
    if(session == null) {
        thrownew Exception("session is null");
    }
     
    //设置登陆主机的密码
    session.setPassword(psw);//设置密码  
    //设置第一次登陆的时候提示,可选值:(ask | yes | no)
    session.setConfig("StrictHostKeyChecking","no");
    //设置登陆超时时间  
    session.connect(30000);
         
    try{
        //创建sftp通信通道
        channel = (Channel) session.openChannel("sftp");
        channel.connect(1000);
        ChannelSftp sftp = (ChannelSftp) channel;
         
         
        //进入服务器指定的文件夹
        sftp.cd("domains");
         
        //列出服务器指定的文件列表
        Vector v = sftp.ls("*.txt");
        for(inti=0;i<v.size();i++){
            System.out.println(v.get(i));
        }
         
        //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
        OutputStream outstream = sftp.put("1.txt");
        InputStream instream = newFileInputStream(newFile("c:/print.txt"));
         
        byteb[] = newbyte[1024];
        intn;
        while((n = instream.read(b)) != -1) {
            outstream.write(b,0, n);
        }
         
        outstream.flush();
        outstream.close();
        instream.close();
    }catch(Exception e) {
        e.printStackTrace();
    }finally{
        session.disconnect();
        channel.disconnect();
    }
}

0 0
原创粉丝点击