// Destroy SFTP Session public void destroySession(ChannelSftp c) throws Exception { try { c.quit(); } catch (Exception exc) { System.err.println("Unable to disconnect from SFTP server. " + exc.toString()); throw exc; } }
public static void main(String[] args) { SFTPGetTest test = new SFTPGetTest(); Map<String, String> sftpDetails = new HashMap<String, String>(); // 设置主机ip,用户名,密码,端口 sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, IP); sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, PORT); sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, USERNAME); sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, PASSWOED); // 创建下载通道。以及超时时长,单位是毫秒 SFTPChannel channel = test.getSFTPChannel(); ChannelSftp chSftp = null; try { chSftp = channel.getChannel(sftpDetails, delayTime); } catch (JSchException e1) { System.out.println("error:创建下载通道失败!failed to creat a channel!"); e1.printStackTrace(); } SftpATTRS attr = null; try { attr = chSftp.stat(FILE_PATH_SFTP); } catch (SftpException e1) { System.out.println("error:获取文件大小失败失败!failed to get the size of the file!"); e1.printStackTrace(); } long fileSize = attr.getSize(); System.out.println("fileSize=" + fileSize); try { chSftp.get(FILE_PATH_SFTP, DESTINATION_LOCAL, new MyProgressMonitor(fileSize)); // 代码段1 // OutputStream out = new FileOutputStream(DESTINATION_LOCAL);//这是采用文件输入流方式下载文件 // chSftp.get(FILE_PATH_SFTP, out, new FileProgressMonitor(fileSize)); // 代码段2 /** * 代码段3 * * <p>OutputStream out = new FileOutputStream(DESTINATION_LOCAL);//这是采用文件输入流方式下载文件 InputStream * is = chSftp.get(FILE_PATH_SFTP, new MyProgressMonitor()); byte[] buff = new byte[1024 * 2]; * int read; if (is != null) { System.out.println("Start to read input stream"); do { read = * is.read(buff, 0, buff.length); if (read > 0) { out.write(buff, 0, read); } out.flush(); } * while (read >= 0); System.out.println("input stream read done."); } */ } catch (Exception e) { e.printStackTrace(); } finally { chSftp.quit(); try { channel.closeChannel(); } catch (Exception e) { System.out.println("error:关闭下载通道失败!failed to close the channel!"); e.printStackTrace(); } } }
@Override public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException { if (!(dir instanceof SFTPPath)) { throw new IllegalArgumentException(dir.toString()); } SFTPHost sftpHost = (SFTPHost) dir.getFileSystem(); String username = sftpHost.getUserName(); String host = sftpHost.getHost(); int port = sftpHost.getPort(); Session session; try { session = jsch.getSession(username, host, port); UserInfo userinfo = new SFTPUserInfo(sftpHost.getPassword()); session.setUserInfo(userinfo); session.connect(); ChannelSftp sftp = (ChannelSftp) session.openChannel(SFTP); sftp.connect(); SFTPPath sftpPath = (SFTPPath) dir; String dirString = sftpPath.getPathString(); try { sftp.mkdir(dirString); } catch (SftpException e) { throw new IOException(dirString, e); } sftp.quit(); session.disconnect(); // throw new UnsupportedOperationException(); } catch (JSchException e) { throw new FileSystemException(e.getMessage()); } }
public static void main(String[] args) throws Exception { SFTPTest test = new SFTPTest(); Map<String, String> sftpDetails = sftpDetails_194; SFTPChannel channel = test.getSFTPChannel(); ChannelSftp chSftp = channel.getChannel(sftpDetails, 60000); System.out.println(">>> >>> begin ...... "); Boolean restart = false; String src = ""; String dst = ""; /** ************************ chetuan ************************* */ // ftl if (ftl_boo) { src = "D:\\workspace_java\\" + app + "\\WebContent\\"; // 本地文件名 dst = tom_path + tomcat + "/webapps/" + app + "/"; // 目标文件名 putDirectory(src, dst, chSftp, contain_ftl); } // class if (class_boo) { src = "D:\\workspace_java\\" + app + "\\build\\classes\\"; dst = tom_path + tomcat + "/webapps/" + app + "/WEB-INF/classes/"; putDirectory(src, dst, chSftp, contain_class); restart = true; } // html if (html_boo) { src = "D:\\workspace_java\\" + app + "\\WebContent\\db12\\css"; // 本地文件名 dst = "/data/app/apache2/hd_html/"; // 目标文件名 putDirectory(src, dst, chSftp, contain_html); } // 重启tomcat if (restart) { ChannelShell channelShell = channel.getChannelShell(); if (channelShell != null) { // 获取输入流和输出流 InputStream instream = channelShell.getInputStream(); OutputStream outstream = channelShell.getOutputStream(); // 发送需要执行的SHELL命令,需要用\n结尾,表示回车 String shellCommand = tom_path + tomcat + "/bin/stop.sh \n"; outstream.write(shellCommand.getBytes()); outstream.flush(); // System.out.println("comm_1 >>> "+getCommRes(instream)); // 缓冲时间 System.out.println(">>> tomcat is bean stop"); Thread.sleep(5 * 1000); // start shellCommand = tom_path + tomcat + "/bin/up.sh \n"; outstream.write(shellCommand.getBytes()); outstream.flush(); System.out.println(">>> tomcat is bean start"); Thread.sleep(5 * 1000); shellCommand = "ps -ef|grep " + tomcat + " |grep -v grep \n"; outstream.write(shellCommand.getBytes()); outstream.flush(); Thread.sleep(5 * 1000); System.out.println("linux: >>> \n" + getCommRes(instream)); outstream.close(); instream.close(); channelShell.disconnect(); } } chSftp.quit(); channel.closeChannel(); System.out.println(">>> >>> done ...... "); }