JSch 是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你可以将它的功能集成到你自己的 程序中。同时该项目也提供一个J2ME版本用来在手机上直连SSHD服务器
Jsch功能很强大,博主这里主要用来做文件操作
1 2 3 4 5 |
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.53</version> </dependency> |
我把我的SftpUtil贴下面了,注释还算清楚
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
/** * Content :sftp协议文件上传下载 * Created by kl on 2016/5/6. */ public class SftpUtil { /** * 上传文件到指定服务器 * @param ip 连接ip * @param user 用户名 * @param psw 密码 * @param port 端口 <=0 为默认端口 * @param fielPath 上传的服务器路径 * @param serverFileName 服务器保存的文件名 * @param instream 要上传的文件流 * @throws Exception */ public static void sftpFileUpload(String ip,int port, String user, String psw, String fielPath,String serverFileName,InputStream instream) throws Exception { Session session =getSession( ip, user, psw, port); Channel channel = null; try { //创建sftp通信通道 channel = (Channel) session.openChannel("sftp"); channel.connect(1000); ChannelSftp sftp = (ChannelSftp) channel; //进入服务器指定的文件夹 sftp.cd(fielPath); OutputStream outstream = sftp.put(serverFileName); byte b[] = new byte[1024*200];//每次传输200k 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(); if (channel!=null)channel.disconnect(); } } /** * 从指定服务器下载文件到本地 * @param ip 连接ip * @param user 用户名 * @param psw 密码 * @param port 端口 <=0 为默认端口 * @param fielPath 服务器文件路径 * @param serverFileName 要下载的文件名 * @param outputStream 输出到本地的文件流 * @throws Exception */ public static void sftpFileDownload(String ip,int port, String user, String psw, String fielPath,String serverFileName,OutputStream outputStream) throws Exception { Session session =getSession( ip, user, psw, port); Channel channel = null; try { //创建sftp通信通道 channel = (Channel) session.openChannel("sftp"); channel.connect(1000); ChannelSftp sftp = (ChannelSftp) channel; //进入服务器指定的文件夹 sftp.cd(fielPath); sftp.get(serverFileName,outputStream); } catch (Exception e) { e.printStackTrace(); } finally { session.disconnect(); if (channel!=null)channel.disconnect(); } } /** * 删除服务器指定文件 * @param ip 连接ip * @param user 用户名 * @param psw 密码 * @param port 端口 <=0 为默认端口 * @param fielPath 服务器文件路径 * @param serverFileName 要删除的文件名 * @throws Exception */ public static void sftpFileDelete(String ip,int port, String user, String psw, String fielPath,String serverFileName) throws Exception { Session session =getSession( ip, user, psw, port); Channel channel = null; try { //创建sftp通信通道 channel = (Channel) session.openChannel("sftp"); channel.connect(1000); ChannelSftp sftp = (ChannelSftp) channel; //进入服务器指定的文件夹 sftp.cd(fielPath); sftp.rm(serverFileName); } catch (Exception e) { e.printStackTrace(); } finally { session.disconnect(); if (channel!=null)channel.disconnect(); } } /** * 查看指定目录所有文件 * @param ip * @param user * @param psw * @param port * @param fielPath * @throws Exception */ public static Vector seeServerFileList(String ip, int port,String user, String psw, String fielPath)throws Exception{ Session session =getSession( ip, user, psw, port); Channel channel = null; Vector v=null; try { //创建sftp通信通道 channel = (Channel) session.openChannel("sftp"); channel.connect(1000); ChannelSftp sftp = (ChannelSftp) channel; //进入服务器指定的文件夹 sftp.cd(fielPath); //列出服务器指定的文件列表 v = sftp.ls(fielPath); } catch (Exception e) { e.printStackTrace(); } finally { session.disconnect(); if (channel!=null)channel.disconnect(); } return v; } /** * 连接服务器 * @param ip 服务器地址 * @param user 用户名 * @param psw 密码 * @param port 连接端口 * @return * @throws Exception */ public static Session getSession(String ip, String user, String psw, int port)throws Exception{ Session session = 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("sftp session is null"); } session.setPassword(psw);//设置密码 //设置登陆超时时间 session.connect(30000);//30s return session; } } |