java:ssh连接服务器,实现本地文件上传和下载

1.连接至服务器:ssh hp@10.10.17.16 -p 5555

   下载文件:scp -r hp@10.10.17.16:/ccc(服务器路径,文件夹下所有文件)  /path(本地路径)

   上传文件:scp /ccc(本地文件路径)

2.文件上传:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class Test {
    public static void main(String[] args) {
        try {
            sshSftp("10.10.17.19", "lenovo", "123456", 5555);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void sshSftp(String ip, String user, String psw ,int port) throws Exception{
        Session session = null;
        ChannelSftp channel = null;
        
        
        JSch jsch = new JSch();
        
        
        if(port <=0){
            //连接服务器,采用默认端口
            session = jsch.getSession(user, ip);
        }else{
            //采用指定的端口连接服务器
            session = jsch.getSession(user, ip ,port);
        }

        //如果服务器连接不上,则抛出异常
        if (session == null) {
            throw new Exception("session is null");
        }
        
        //设置第一次登陆的时候提示,可选值:(ask | yes | no)
        session.setConfig("StrictHostKeyChecking", "no");
        //设置登陆主机的密码
        session.setPassword(psw);//设置密码   
    
        //设置登陆超时时间   
        session.connect();
        
        try {
            //创建sftp通信通道
            channel = (ChannelSftp)           session.openChannel("sftp");
            channel.connect(1000);
            ChannelSftp sftp = (ChannelSftp) channel;
            
            
            //进入服务器指定的文件夹
            sftp.cd("1047");
            
            //列出服务器指定的文件列表
            Vector v = sftp.ls("*.txt");
            for(int i=0;i<v.size();i++){
                System.out.println(v.get(i));
            }
            
            //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
            OutputStream outstream = sftp.put("666666666.txt");
            InputStream instream = new FileInputStream(new File("c:/print.txt"));
            
            byte b[] = new byte[1024];
            int n;
            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();
        }
    }
}
    

3.文件下载:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;


public class Test2 {
    public static void main(String[] args) {
        try {
            sshSftp("10.10.17.19", "lenovo", "123456", 5555);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void sshSftp(String ip, String user, String psw ,int port) throws Exception{
        Session session = null;
        ChannelSftp channel = null;
        
        
        JSch jsch = new JSch();
        
        
        if(port <=0){
            //连接服务器,采用默认端口
            session = jsch.getSession(user, ip);
        }else{
            //采用指定的端口连接服务器
            session = jsch.getSession(user, ip ,port);
        }

        //如果服务器连接不上,则抛出异常
        if (session == null) {
            throw new Exception("session is null");
        }
        
        //设置第一次登陆的时候提示,可选值:(ask | yes | no)
        session.setConfig("StrictHostKeyChecking", "no");
        //设置登陆主机的密码
        session.setPassword(psw);//设置密码   
    
        //设置登陆超时时间   
        session.connect();
        
        try {
            //创建sftp通信通道
            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect(1000);
            ChannelSftp sftp = (ChannelSftp) channel;
            
            
            //进入服务器指定的文件夹
            sftp.cd("1047");
            
            //列出服务器指定的文件列表
            Vector v = sftp.ls("*.txt");
            for(int i=0;i<v.size();i++){
                System.out.println(v.get(i));
            }
            
            //下载
            InputStream instream = sftp.get("666666666.txt");
            OutputStream outstream = new FileOutputStream(new File("c:/print.txt"));
            
            byte b[] = new byte[1024];
            int n;
            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();
        }
    }
}
原文地址:https://www.cnblogs.com/kuangzhisen/p/8662816.html