若是你想用纯JAVA实现SFTP文件上传或下载,或者是想纯JAVA链接到SSH2服务器上执行命令,那就使用JSch.jar包吧。这里咱们先描述实现SFTP协议的上传和下载,上代码和详细的代码注释: java
/** * 利用JSch包实现SFTP下载、上传文件 * @param ip 主机IP * @param user 主机登录用户名 * @param psw 主机登录密码 * @param port 主机ssh2登录端口,若是取默认值,传-1 */ public static void sshSftp(String ip, String user, String psw ,int port) throws Exception{ Session session = null; Channel 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"); } //设置登录主机的密码 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(int i=0;i<v.size();i++){ System.out.println(v.get(i)); } //如下代码实现从本地上传一个文件到服务器,若是要实现下载,对换如下流就能够了 OutputStream outstream = sftp.put("1.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(); } }
另外,JSCH也支持密钥的方式登录,只需在jsch.getSession以前设置一下密钥的相关信息就能够了,如下是带密钥的代码: 服务器
/** * 利用JSch包实现SFTP下载、上传文件 * @param ip 主机IP * @param user 主机登录用户名 * @param psw 主机登录密码 * @param port 主机ssh2登录端口,若是取默认值(默认值22),传-1 * @param privateKey 密钥文件路径 * @param passphrase 密钥的密码 * */ public static void sshSftp(String ip, String user, String psw ,int port ,String privateKey ,String passphrase) throws Exception{ Session session = null; Channel channel = null; JSch jsch = new JSch(); //设置密钥和密码 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) { throw new 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(int i=0;i<v.size();i++){ System.out.println(v.get(i)); } //如下代码实现从本地上传一个文件到服务器,若是要实现下载,对换如下流就能够了 OutputStream outstream = sftp.put("1.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(); } }