ssh2 java_SSH2纯Java实现—JSch介绍和使用

1 packagecn.zxj.gkxt.core.util;2

3

4 import com.jcraft.jsch.*;5 importorg.slf4j.Logger;6 importorg.slf4j.LoggerFactory;7

8

9 importjava.io.File;10 importjava.io.FileInputStream;11 importjava.io.IOException;12 importjava.io.InputStream;13 import java.util.*;14

15

16 /**

17 * JSch(Java Secure Channel)18 * SSH2纯Java实现19 * 连接至SSH服务器20 */

21 public classJschUtils {22 private static final Logger LOG = LoggerFactory.getLogger(JschUtils.class.getName());23

24

25 /**

26 * main方法27 * 本地开发28 *@paramargs 输入参数29 */

30 public static voidmain(String[] args) {31 String userName = System.getProperty("user.name");32

33 ChannelSftp sftp = connect("10.194.61.92", 22, "tomcat", "Gzyqzxj092@tomcatlinserver");34

35

36 //上传文件至SFTP指定目录

37 if (1 == 1) {38 upload("/home/tomcat/gkxt/static/sftpLibrary", "C:\\Users\\" + userName + "\\Documents\\demo.jpg", sftp);39 }40 }41

42

43 /**

44 * 连接SFTP服务器45 *@paramhost 主机46 *@paramport 端口47 *@paramusername 用户名48 *@parampassword 密码49 *@returnChannelSftp50 */

51 public static ChannelSftp connect(String host, intport, String username,52 String password) {53 ChannelSftp sftp = null;54

55

56 try{57 //初始化JCsh对象

58 JSch jsch = newJSch();59 //由用户名 | 主机ip | 端口号获取单个Session对象

60 Session sshSession =jsch.getSession(username, host, port);61 LOG.info("Session created.");62

63

64 //设置登录密码

65 sshSession.setPassword(password);66

67

68 //初始化Properties对象

69 Properties sshConfig = newProperties();70 sshConfig.put("StrictHostKeyChecking", "no");71

72

73 //为Session对象设置properties

74 sshSession.setConfig(sshConfig);75 //设置timeout

76 sshSession.setTimeout(5000);77 sshSession.connect();78 LOG.info("Session connected.");79 LOG.info("Opening Channel.");80

81

82 //指定获取Channel连接方式

83 Channel channel = sshSession.openChannel("sftp");84 channel.connect();85 sftp =(ChannelSftp) channel;86 LOG.info("Connected to " + host + ".");87 } catch(Exception e) {88 e.printStackTrace();89 throw newRuntimeException(e);90 }91 returnsftp;92 }93

94

95 /**

96 * 上传文件97 *@paramdirectory 上传的目录98 *@paramuploadFile 要上传的文件99 *@paramsftp ChannelSftp100 */

101 public static voidupload(String directory, String uploadFile, ChannelSftp sftp) {102 mkDir(directory, sftp);103 InputStream in = null;104 try{105 sftp.cd(directory);106 File file = newFile(uploadFile);107 in = newFileInputStream(file);108 sftp.put(newFileInputStream(file), file.getName());109 } catch(Exception e) {110 e.printStackTrace();111 } finally{112 try{113 assert in != null;114 in.close();115 } catch(IOException e) {116 e.printStackTrace();117 }118 }119 }120

121

122 /**

123 * 下载文件124 *@paramdirectory 目录125 *@paramdownloadFile 下载的文件126 *@paramsftp ChannelSftp127 */

128 public staticInputStream download(String directory, String downloadFile,129 ChannelSftp sftp) {130 InputStream in = null;131 try{132 sftp.cd(directory);133 in =sftp.get(downloadFile);134 } catch(Exception e) {135 e.printStackTrace();136 throw newRuntimeException(e);137 }138 returnin;139 }140

141

142 /**

143 * 删除文件144 *@paramdirectory 文件所在目录145 *@paramdeleteFile 要删除的文件146 *@paramsftp ChannelSftp147 */

148 public static voiddelete(String directory, String deleteFile, ChannelSftp sftp) {149 try{150 sftp.cd(directory);151 sftp.rm(deleteFile);152 } catch(Exception e) {153 e.printStackTrace();154 throw newRuntimeException(e);155 }156 }157

158

159 /**

160 * 列出目录下的文件161 *@paramdirectory 要列出的目录162 *@paramsftp ChannelSftp163 *@returnVector(动态数组)164 *@throwsSftpException Sftp异常165 */

166 public static Vector listFiles(String directory, ChannelSftp sftp) throwsSftpException {167 returnsftp.ls(directory);168 }169

170

171

172 /**

173 * 获取目录名称174 *@paramls 目录数组175 *@return目录名称集合176 */

177 public static ListbuildFiles(Vector ls) {178 if (ls != null) {179 List list = new ArrayList<>();180 for(Object l : ls) {181 ChannelSftp.LsEntry f =(ChannelSftp.LsEntry) l;182 String nm =f.getFilename();183 if (".".equals(nm) || "..".equals(nm)) {184 continue;185 }186 list.add(nm);187 }188

189

190 returnlist;191 }192

193

194 return null;195 }196

197

198 /**

199 * 打开指定目录200 *@paramdirectory 目录201 *@returnboolean202 */

203 public static booleanopenDir(String directory, ChannelSftp sftp) {204 try{205 sftp.cd(directory);206 return true;207 } catch(SftpException e) {208 return false;209 }210 }211

212

213 /**

214 * 按指定路径创建文件夹215 *@paramdirName 文件夹路径216 *@paramsftp ChannelSftp217 */

218 public static voidmkDir(String dirName, ChannelSftp sftp) {219 String[] dirs = dirName.split("/");220 try{221 String now =sftp.pwd();222 sftp.cd("/");223 for(String dir : dirs) {224 if(StringUtils.isNotEmpty(dir)) {225 boolean dirExists =openDir(dir, sftp);226 if (!dirExists) {227 sftp.mkdir(dir);228 sftp.cd(dir);229 }230 }231 }232 sftp.cd(now);233 } catch(SftpException e) {234 e.printStackTrace();235 }236 }237 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值