当前位置: 首页 > 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.hkea.cn/news/1406/

相关文章:

  • 汉南做网站南京疫情最新情况
  • web网站开发全过程网站开发步骤
  • 程序员 创业做网站关键词优化是怎么弄的
  • 做网站其实不贵搜索引擎竞价推广的优势
  • 邢台网站建设哪家好互联网营销师培训大纲
  • 南京江宁区住房建设局网站网站做优化
  • 常熟做网站公司排名百度怎样发布信息
  • 12360官方网站下载seo排名大概多少钱
  • 网站反向代理怎么做网站优化培训班
  • wordpress导航链接中山百度seo排名公司
  • 唐山网站设计制作网络推广优化seo
  • 做网站怎样申请动态域名网络营销推广策划的步骤是什么
  • 厦门启明星网站建设潍坊网站外包
  • 做淘宝客没有网站怎么做重庆的seo服务公司
  • 推广网站可以做跳转吗百度排行榜
  • 用什么网站做一手房最好百度经验官网首页
  • 沈阳建设网站韩国网站
  • 温州中小企业网站制作搜索引擎营销的优势和劣势
  • 求推荐做ppt的网站网络营销专业是干什么的
  • 做婚庆网站的功能定位windows优化大师卸载不掉
  • 养老网站建设泰安网站seo
  • 做现货黄金的金融网站黄山网站建设
  • 龙湾区建设局的网站在线客服
  • 旅游网站开发设计百度收录量查询
  • 做外链选择那些网站seo研究中心学员案例
  • 网站微信建设运维经验湖北seo诊断
  • 华夏业务员做单的网站怎么营销一个产品
  • 黄色的html代码情感网站seo
  • 网站推广的方式有微信附近人推广引流
  • 网站后台管理界面代码网站关键词查询