//本程序的作用:对远程服务器的目录,实时的监控,并且递归下载,如果哪个方法你不懂的话,哈哈那你百度一下。代码是可以用的哦。 package com.tp; import com.jcraft.jsch.*; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Timer; import java.util.TimerTask; import java.util.Vector; /** * 类说明 sftp工具类 */ public class SFTPUTIL { private static ChannelSftp sftp; //"/home/mysftp"; // public static final String directory="/home/voipData/"; public static final String directory="/root/list"; private Session session; /** SFTP 登录用户名*/ private String username; /** SFTP 登录密码*/ private String password; /** 私钥 */ private String privateKey; /** SFTP 服务器地址IP地址*/ private String host; /** SFTP 端口*/ private int port; /** * 构造基于密码认证的sftp对象 */ public SFTPUTIL(String username, String password, String host, int port) { this.username = username; this.password = password; this.host = host; this.port = port; } /** * 构造基于秘钥认证的sftp对象 */ public SFTPUTIL(String username, String host, int port, String privateKey) { this.username = username; this.host = host; this.port = port; this.privateKey = privateKey; } public SFTPUTIL(){} /** * 连接sftp服务器 */ public void login(){ try { JSch jsch = new JSch(); if (privateKey != null) { jsch.addIdentity(privateKey);// 设置私钥 } session = jsch.getSession(username, host, port); if (password != null) { session.setPassword(password); } Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; } catch (JSchException e) { e.printStackTrace(); } } /** * 关闭连接 server */ public void logout(){ if (sftp != null) { if (sftp.isConnected()) { sftp.disconnect(); } } if (session != null) { if (session.isConnected()) { session.disconnect(); } } } //获取所有的数据目录放入list集合之中 public void test(String directory) throws Exception { if (isDirectory(directory)){ Vector<ChannelSftp.LsEntry> sftpFile = sftp.ls(directory); for(ChannelSftp.LsEntry lsEntry:sftpFile){ if (lsEntry.getFilename().length()>2){ // System.out.println("这是目录"+lsEntry.getFilename()); System.out.println(directory +"/"+ lsEntry.getFilename()); test(directory +"/"+ lsEntry.getFilename()); } } }else { System.out.println(directory); File file = new File(System.getProperty("outPutPath")+directory).getParentFile(); if(!file.exists()){ file.mkdirs(); } try { sftp.get(directory, System.getProperty("outPutPath") + directory); }catch (Exception e){ System.out.println("下载出现异常》》》》》》》》》》》》即将退出程序"); return; } try{ sftp.rm(directory); }catch (Exception e){ e.printStackTrace(); System.out.println("删除"+directory+"出现异常"); } } // Vector<ChannelSftp.LsEntry> sftpFile = sftp.ls(directory); // for(ChannelSftp.LsEntry lsEntry:sftpFile){ } public boolean isDirectory(String directory) throws Exception { boolean isDirExistFlag = false; try{ SftpATTRS sftpATTRS = sftp.lstat(directory); //lstat()检索文件或目录的文件属性 isDirExistFlag = true; // System.out.println(sftpATTRS.isDir()); return sftpATTRS.isDir(); //isDir()检查此文件是否为目录 }catch (Exception e){ throw new Exception("检查目录不存在"); } } /** * 将输入流的数据上传到sftp作为文件。文件完整路径=basePath+directory * @param basePath 服务器的基础路径 * @param directory 上传到该目录 * @param sftpFileName sftp端文件名 */ public void upload(String basePath,String directory, String sftpFileName, InputStream input) throws SftpException { try { sftp.cd(basePath); sftp.cd(directory); } catch (SftpException e) { //目录不存在,则创建文件夹 String [] dirs=directory.split("/"); String tempPath=basePath; for(String dir:dirs){ if(null== dir || "".equals(dir)) continue; tempPath+="/"+dir; try{ sftp.cd(tempPath); }catch(SftpException ex){ sftp.mkdir(tempPath); sftp.cd(tempPath); } } } sftp.put(input, sftpFileName); //上传文件 } /* * @author pg * @date 2019-01-21 16:07 * @param * @return */ public Vector<?> listFiles(String directory) throws SftpException { return sftp.ls(directory); } public static void TimeSchduler(final SFTPUTIL sf){ TimerTask task = new TimerTask() { @Override public void run() { System.out.println("新的定时"); try { sf.test(directory); } catch (Exception e) { System.out.println("检擦目录模块出现异常"+e.getMessage()); e.printStackTrace(); } } }; Timer timer = new Timer(); long delay = 0; long intevalPeriod = 10 * 1000; // schedules the task to be run in an interval timer.scheduleAtFixedRate(task, delay, intevalPeriod); // sf.logout();//定时扫描目录的话这个方法要注释掉 } //上传文件测试 qmwneb123 47.244.113.15 public static void main(String[] args) throws SftpException, IOException { final SFTPUTIL sf = new SFTPUTIL("root", "qazQWE123!@#", "外网ip", 22); sf.login(); if(args.length!=1){ System.out.println("输出路径错误,请重新传入,仅支持一个参数路径 程序即将退出"); return; } System.setProperty("outPutPath",args[0]); TimeSchduler(sf); // sf.logout();定时扫描目录的话这个方法要注释掉 } }
jsch下载文件(递归下载)
最新推荐文章于 2024-01-10 10:35:20 发布