当前位置: 首页 > news >正文

江阴网站开发门户网站建设情况简介

江阴网站开发,门户网站建设情况简介,公众号开发者密码忘了怎么办,耐克网站建设策划方案序言 需要读取sftp服务器上符合指定的文件名正则的文件列表&#xff0c;目前想到的最好的办法就是递归。 我这里引入的依赖是&#xff1a; <!-- jsch-sftp连接 --><dependency><groupId>com.jcraft</groupId><artifactId>jsch</artif…

序言

需要读取sftp服务器上符合指定的文件名正则的文件列表,目前想到的最好的办法就是递归。

我这里引入的依赖是:

        <!--   jsch-sftp连接     --><dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.54</version></dependency>

不废话直接上代码:

  public static List<String> getFileList(ChannelSftp channelSftp, String path, String fileNamePattern, Integer filePathDepth) {List<String> fileList = Lists.newLinkedList();try {Pattern pattern = Pattern.compile(fileNamePattern);Vector<ChannelSftp.LsEntry> files = channelSftp.ls(path);//读取的根路径下一级就是文件if (1 == filePathDepth) {for (ChannelSftp.LsEntry entry : files) {String fileName = entry.getFilename();//找到和规则(文件名正则)匹配的文件if (pattern.matcher(fileName).matches()) {//拼接全路径String fullPath = path + fileName;fileList.add(fullPath);}}} else {//从读取根路径下开始算目录深度时,目录深度大于1就使用递归来读取文件列表manyDirFileList(channelSftp, path, fileNamePattern, fileList, bComFilesaveReadruleDO.getDirPattern());}} catch (Exception e) {log.error("获取sftp指定目录下的文件列表失败,{}", e.getMessage());}return fileList;}/*** 递归获取提供的路径下多级目录下符合正则的所有文件** @param channelSftp ftp对象* @param path        路径* @param fileList    文件列表**/public static void manyDirFileList(ChannelSftp channelSftp, String path, String fileNamePattern,List<String> fileList, String dirPattern) throws Exception {try {List<Pattern> dirPatterns = new ArrayList<>();if (StringUtils.isNotEmpty(dirPattern)) {for (String pat : dirPattern.split(",")) {dirPatterns.add(Pattern.compile(pat.trim()));}}Pattern fileNamePat = Pattern.compile(fileNamePattern);if (isDirectory(channelSftp, path)) {Vector<?> vector = channelSftp.ls(path);for (Object l : vector) {ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) l;String fileName = file.getFilename();boolean isDirMatch = dirPatterns.isEmpty();for (Pattern dirPat : dirPatterns) {if (dirPat.matcher(fileName).matches()) {isDirMatch = true;break;}}if (fileName.equals(".") || fileName.equals("..")) {continue;}if (isDirMatch || fileNamePat.matcher(fileName).matches()) {String fullPath = path + fileName + (file.getAttrs().isDir() ? "/" : "");manyDirFileList(channelSftp, fullPath, fileNamePattern, fileList, dirPattern);}}} else {String fileName = path.substring(path.lastIndexOf("/") + 1);if (fileNamePat.matcher(fileName).matches()) {fileList.add(path);}}} catch (SftpException e) {log.error("获取FTP指定目录下的文件异常,路径:{},异常信息:{}", path, e.getMessage());} catch (Exception e) {log.error("递归获取SFTP指定目录下的文件列表失败,路径:{},异常信息:{}", path, e.getMessage());throw new Exception("递归获取SFTP指定目录下的文件列表失败,路径:" + path + ",异常信息:" + e.getMessage());}}

另外还有一个需求就是只读取10个文件:

 public static List<String> getFileListFor10(ChannelSftp channelSftp,  String path, String fileNamePattern, Integer filePathDepth) {List<String> fileList = Lists.newLinkedList();try {Pattern pattern = Pattern.compile(fileNamePattern);Vector<ChannelSftp.LsEntry> files = channelSftp.ls(path);//读取的根路径下一级就是文件if (1 == filePathDepth) {for (ChannelSftp.LsEntry entry : files) {if (fileList.size() > 10) {log.info("已读取10个文件,不再读取目录:{}下的文件", path);break;}String fileName = entry.getFilename();//找到和规则(文件名正则)匹配的文件if (pattern.matcher(fileName).matches()) {//拼接全路径String fullPath = path + fileName;fileList.add(fullPath);}}} else {//从输入的根路径下开始算目录深度时,目录深度大于1就使用递归来读取文件列表manyDirFileListFor10(channelSftp, path, fileNamePattern, fileList, bComFilesaveReadruleDO.getDirPattern());}} catch (Exception e) {log.error("获取sftp指定目录下的文件列表失败,{}", e.getMessage());}return fileList;}/*** 递归获取提供的路径下多级目录下符合正则的前10个文件** @param channelSftp ftp对象* @param path        路径* @param fileList    文件列表**/public static void manyDirFileListFor10(ChannelSftp channelSftp, String path, String fileNamePattern,List<String> fileList, String dirPattern) {try {List<Pattern> dirPatterns = new ArrayList<>();if (StringUtils.isNotEmpty(dirPattern)) {for (String pat : dirPattern.split(",")) {dirPatterns.add(Pattern.compile(pat.trim()));}}Pattern fileNamePat = Pattern.compile(fileNamePattern);if (isDirectory(channelSftp, path)) {Vector<?> vector = channelSftp.ls(path);for (Object o : vector) {// 如果已经找到了10个文件,直接返回,不再递归if (fileList.size() >= 10) {log.info("已读取10个文件,不再读取目录:{}下的文件", path);break;}ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) o;String fileName = file.getFilename();boolean isDirMatch = dirPatterns.isEmpty();for (Pattern dirPat : dirPatterns) {if (dirPat.matcher(fileName).matches()) {isDirMatch = true;break;}}if (fileName.equals(".") || fileName.equals("..")) {continue;}if (isDirMatch || fileNamePat.matcher(fileName).matches()) {String fullPath = path + fileName + (file.getAttrs().isDir() ? "/" : "");manyDirFileListFor10(channelSftp, fullPath, fileNamePattern, fileList, dirPattern);}}} else {String fileName = path.substring(path.lastIndexOf("/") + 1);if (fileNamePat.matcher(fileName).matches()) {fileList.add(path);}}} catch (SftpException e) {log.error("获取FTP指定目录下的文件异常,路径:{},异常信息:{}", path, e.getMessage());} catch (Exception e) {log.error("递归获取SFTP指定目录下的文件列表失败,路径:{},异常信息:{}", path, e.getMessage());}}

-----------------知道的越多, 不知道的越多--------------------

http://www.yayakq.cn/news/681478/

相关文章:

  • 公司网站公众号维护怎么做wordpress 标签模板下载
  • 校园二手网站开发的意义广西工程建设质量管理协会网站
  • 电商网站的银行支付接入该怎么做百度提交
  • 网站上线 串词wordpress word上传
  • 常州交通建设管理有限公司网站北京广告设计公司
  • 柯林建站程序大宗商品现货交易规则
  • 低功耗集成主板做网站让顾客进店的100条方法
  • 网站建设_app开发赣州网红打卡旅游景点
  • 查看一个网站的源代码做评价苏州企业排名
  • 网站建设功能的策划书网站开发职业要求
  • 网站建设平台官网河间米各庄视频推广软件
  • 南山网站优化短视频拍摄剪辑培训班
  • 遂宁网站建设公司哪家好腾讯云做视频网站
  • 云南省建设考试中心网站怎样找网站
  • 母版页做网站例子手机上怎么安装wordpress
  • 网站推广文案wordpress html5 主题
  • 浙江公司网站建设推广手工制作小玩具简单又好玩
  • 技能培训中心网站建设seo教程pdf
  • 网站主页面设计扬州工程建设信息网站
  • 如何看网站是否有做网站地图离线发布wordpress
  • 建设数据库网站需要哪些设备漳州做网站
  • 全能网站服务器上海网络推广排名公司
  • 深圳网站建设deyond网站建设推广机构
  • 手机微网站尺寸自己做公司的网站
  • 内蒙古兴安盟建设局网站微信小程序开发框架
  • 武侯区建设局门户网站手机版刷赞网站建设
  • jsp购物网站开发视频wordpress详细介绍
  • 手机网站有哪些网站友情链接出售
  • 自助餐火锅网站建设哪里长沙网站开发
  • 服装做外贸的网站建设做网站 做手机app要学什么软件