ftp文件采集

2023-11-08

本地文件根据文件名过滤




import org.springframework.stereotype.Service;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 * @author Jack Que
 * @ClassName: RadarMapServiceImpl
 * @Description:
 * @Author
 * @Version 1.0
 */
@Service
public class RadarMapServiceImpl implements RadarMapService {

    /**
     * 获取路径下的所有文件/文件夹
     *
     * @param directoryPath 需要遍历的文件夹路径
     * @param startDay      the start day
     * @param endDay        the end day
     * @return all file
     * @author Jack Que
     * @created 2021 -05-21 16:07:23
     */
    public static List<RadarMapDto.DataMap> getAllFile(String directoryPath, String startDay, String endDay) {
        if (directoryPath == null || directoryPath.isEmpty()) {
            return new ArrayList<>();
        }
        List<RadarMapDto.DataMap> list = new ArrayList<>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        for (File file : files) {
            if (file.isDirectory() && (endDay.compareTo(file.getName()) >= 0 && startDay.compareTo(file.getName()) <= 0)) {
                //递归调用
                list.addAll(getAllFile(file.getAbsolutePath()));
            }
        }
        return list;
    }

    /**
     * 递归遍历出所有图片信息 Gets all file.
     *
     * @param directoryPath the directory path
     * @return the all file
     * @author Jack Que
     * @created 2021 -05-21 16:07:23
     */
    public static List<RadarMapDto.DataMap> getAllFile(String directoryPath) {
        List<RadarMapDto.DataMap> list = new ArrayList<>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                //递归调用
                list.addAll(getAllFile(file.getAbsolutePath()));
            } else {
                RadarMapDto.DataMap dataMap = new RadarMapDto.DataMap();
                dataMap.setName(file.getName());
                dataMap.setSrc(file.getAbsolutePath().replace("\\", "//"));
                list.add(dataMap);
            }
        }
        return list;
    }

    /**
     * 获取文件路径 Gets file path.
     *
     * @param startTime the start time
     * @param endTime   the end time
     * @param homeCity  the home city
     * @param path      the path
     * @return the file path
     * @author Jack Que
     * @created 2021 -05-21 16:07:23
     */
    public static List<RadarMapDto.DataMap> getFilePath(String startTime, String endTime, String homeCity, String path) {
        if (startTime.length() < 8 && endTime.length() < 8) {
            return new ArrayList<>();
        }
        String startMonth = startTime.substring(0, 6);
        String startDay = startTime.substring(6, 8);
        String endMonth = endTime.substring(0, 6);
        String endDay = endTime.substring(6, 8);
        List<RadarMapDto.DataMap> listPath = new ArrayList<>();
        //匹配地市
        String homeCityFilePath = getFilePath(homeCity, path);

        //判断是否跨月
        if (Objects.equals(startMonth, endMonth) && !homeCityFilePath.isEmpty()) {
            //只有一个月的时候去取出
            String filePath = getFilePath(startMonth, homeCityFilePath);
            listPath.addAll(getAllFile(filePath, startDay, endDay));
        } else {
            //跨月取出
            String fileStartPath = getFilePath(startMonth, homeCityFilePath);
            listPath.addAll(getAllFile(fileStartPath, startDay, "31"));
            String fileEndPath = getFilePath(endMonth, homeCityFilePath);
            listPath.addAll(getAllFile(fileEndPath, "01", endDay));
        }
        return filterFile(startTime, endTime, listPath);
    }

    /**
     * 获取对应目录 Gets file path.
     *
     * @param pathName the path name
     * @param path     the path
     * @return the file path
     * @author Jack Que
     * @created 2021 -05-21 16:07:23
     */
    private static String getFilePath(String pathName, String path) {
        if (path==null||path.isEmpty()) {
            return "";
        }
        File baseFile = new File(path);
        if (baseFile.isFile() || !baseFile.exists()) {
            return "";
        }
        for (File file : baseFile.listFiles()) {
            if (file.isDirectory() && Objects.equals(pathName, file.getName())) {
                return file.getAbsolutePath();
            }
        }
        return "";
    }

    /**
     * 过滤 符合时间内的图片.
     *
     * @param startTime the start time
     * @param endTime   the end time
     * @param files     the files
     * @return the list
     * @author Jack Que
     * @created 2021 -05-21 16:07:23 Filter file list.
     */
    public static List<RadarMapDto.DataMap> filterFile(String startTime, String endTime, List<RadarMapDto.DataMap> files) {
        //先过滤符合时间的,后面返回文件格式化后结果
        return files.stream().filter(item -> {
                    String[] filename = item.getName().split("\\.");
                    if (filename.length < 2) {
                        return true;
                    }
                    //文件后缀匹配
                    if ("PNG".equals(filename[1]) || "png".equals(filename[1])) {
                        return endTime.compareTo(filename[0]) > 0 && startTime.compareTo(filename[0]) <= 0;
                    }
                    return true;
                }
        ).map(item->{
            String[] filename = item.getName().split("\\.");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
            SimpleDateFormat newSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                item.setName(newSdf.format(sdf.parse(filename[0])));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //文件名格式化返回
            return item;
        }).collect(Collectors.toList());
    }


    /**
     * 描述 Gets radar map list.
     *
     * @param startTime the start time
     * @param endTime   the end time
     * @param homeCity  the home city
     * @return the radar map list
     * @author Jack Que
     * @created 2021 -05-21 16:07:23
     */
    @Override
    public List<RadarMapDto.DataMap> getRadarMapList(String startTime, String endTime, String homeCity) {
        //todo 后面path要写进配置中
        String path = "D:\\代码\\wrpt-business-front\\public\\static\\img\\";
        return getFilePath(startTime, endTime, homeCity, path);
    }

    @Override
    public String getRadarMapList(String homeCity) {
        //todo 后面path要写进配置中
        String path = "D:\\代码\\wrpt-business-front\\public\\static\\img\\"+homeCity;
        return getMaxFilePath(path);
    }

    public String getMaxFilePath( String path) {
        File baseFile = new File(path);
        if (baseFile.isFile() || !baseFile.exists()) {
            return "";
        }
        List<String> listDirectoryName = new ArrayList<>();
        for (File file : baseFile.listFiles()) {
            //判断是否为月份文件夹
            if (file.isDirectory()&&file.getName().length()==6) {
                listDirectoryName.add(file.getName());
            }
            //判断是否为天数文件夹
            if (file.isDirectory()&&file.getName().length()==2) {
                listDirectoryName.add(file.getName());
            }
        }
        //
        List<String> collect = listDirectoryName.stream().sorted((s1, s2) -> s2.compareTo(s1)).collect(Collectors.toList());

        if(!collect.isEmpty()&&collect.get(0).length()==6){
            for (String fileName : collect) {
                return getMaxFilePath(path+"\\"+fileName);
            }
        }

        if(!collect.isEmpty()&&collect.get(0).length()==2){
            for (String fileName : collect) {
                File files = new File(path,fileName);
                for (File file : files.listFiles()) {
                    if (file.isFile()) {
                        return path.substring(path.length()-6,path.length()-2)+"-"+path.substring(path.length()-2,path.length())+"-"
                                +collect.get(0);
                    }
                }
            }
        }
        return "";
    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ftp文件采集 的相关文章

随机推荐

  • qt没有mysql驱动的解决办法

    qt没有mysql驱动的解决办法 第一部分 qtcreator上没有mysql驱动的解决办法 第一步 找到你的qt的版本的源码src 第二步 点击mysql pro 电脑会自动打开qtcreater 然后就是进行编译器的选择 我选择的是 在
  • BootStrap的使用

    是别人帮我们已经写好的css样式 我们如果想要使用这个BootStrap 下载BootStrap 使用 在页面上引入BootStrap 自定置 先在网上下载好BootStrap 并导入到Pycharm 引入BootStrap 注意引入的是
  • 【react】文本内容超过一行,显示为单行省略,并且出现icon图标;点击此图标,可以进行展开或收起文本功能实现

    需求 多条数据展示 每条数据的文本内容不超过一行 文本内容为一行时 不显示 展开收起icon图标 文本超过一行时 内容单行省略 并且显示 点击图标 图标切换为收起按钮 后端返回数据 const data name 测试测试测试 time 2
  • BinaryViewer(二进制查看器)使用教程(附下载)

    1 BinaryViewer操作界面 2 面板功能 1 数据面板 此面板占据了屏幕的最中央部分 其目的是顺序显示打开的文件或物理驱动器中的所有数据 此面板通常以两列显示数据 每列都可以按用户选择的格式显示数据 请转到数据显示模式 查看如何更
  • 对indexedDB的一些使用方法

    indexedDB的使用 1 打开数据库和创建数据仓库 createDB function dbName version tableName key cursor callBack 参数为 dbName数据库名 version版本号 tab
  • Python运维开发工程师养成记(while循环语句)

    图示 案例 contine和break用法 无限循环 while else语句 今天分享到这里 喜欢的盆友可以关注一下博主 链接 https ke qq com course 4300856 tuin d8aedf68
  • android 环信集成,Android 环信集成使用总结

    最近因为项目需要 需要集成环信 对于一些账号的注册 配置的添加官方文档上写的都有 就不在记录 就记录一下集成过程中遇到的问题 环信demo中的代码太乱 而且一些功能用不到 我们就移值些自己有用的放到自己的项目中 1 消息监听 环信在收到消息
  • mysql如何查询成绩前5名_sql 语句查询 前5名后5名的成绩

    蝴蝶不菲 两种办法 分别求最大和最小 然后union allselect from select from table order by 成绩 where rownum lt 5union allselect from select fro
  • 每隔5分钟输出最近一小时内点击量最多的前N个商品(SQL实现版)

    代码 package com zjc flow analysis hotitems analysis import org apache flink api common serialization SimpleStringSchema i
  • 智能合约调试指南

    不像你在其他地方看到的纸质合约 以太坊的智能合约是代码组成的 需要你以非常谨慎的态度去对待它 这是一件好事 想象下如果现实世界的合同需要编译的话会更清晰么 如果我们的合同没有被正确的编码出来 我们的交易可能会失败 导致以太币的损失 以 ga
  • 真题详解(Flynn分类)-软件设计(四十六)

    真题详解 计算机总线 软件设计 四十五 https blog csdn net ke1ying article details 130046829 Flynn分类将计算机分为四类 单指令流单数据流机器 SISD 早期的机器 在某个时钟周期
  • C++ 读取结束的判断

    cin 可以用来从键盘输入数据 将标准输入重定向为文件后 cin 也可以用来从文件中读入数据 在输入数据的多少不确定 且没有结束标志的情况下 该如何判断输入数据已经读完了呢 从文件中读取数据很好办 到达文件末尾就读取结束了 从控制台读取数据
  • shell脚本系列:6、shell扩展

    shell脚本系列 6 shell扩展 文章目录 shell脚本系列 6 shell扩展 1 花括号扩展 2 波浪号扩展 3 shell参数扩展 4 命令替换 5 算术扩展 6 进程替换 7 分词 8 文件名扩展 8 1 模式匹配 9 引号
  • 【upload-labs】————8、Pass-07

    闯关界面 前后端检测判断 查看源代码 文件后缀大小写 去除 DATA 关键词过滤 在这里可以发现所有的都考虑到了 但是却没有考虑后缀为 的情况 在windows中PHP会自动去除后缀名中最后的 我们可以通过这种方式来绕过 加 来绕过
  • 探索多线程使用同一个数据库connection的后果

    在项目中看到有用到数据库的连接池 心里就思考着为什么需要数据库连接池 只用一个连接会造成什么影响 只用一个connection 1 猜想 jdbc的事务是基于connection的 如果多线程共用一个connection 会造成多线程之间的
  • C++安装Dlib库教程(保姆级别)以及踩坑指南

    在网上搜索了一圈 发现大家好像都很喜欢使用Python来开发AI 后来我也用了一下Python 发现 emmm 真香 但是我知道一定也有人再使用C 进行开发 那么我就先来说说几种安装Dlib库的方法趴 除了使用vcpkg 我们这次从官网的角
  • 微信小程序自定义底部导航栏

    文章目录 概要 功能 源码 细节 改进 概要 微信小程序自定义底部导航栏 原生实现 不包含其他任何第三方组件 比较干净 开箱即用 效果预览 功能 可自定义底部导航栏列表样式 可自定义每个菜单的默认 激活后的图标和文字样式 可自定义是否添加中
  • 【gitlab项目迁移】

    需求 将gitlab项目从A组迁移到B组 经查 有两种方式 一种是项目在网页压缩后export 再import 另一种是终端操作 但是我的项目分支过多 文件过大 30M左右 方法一会报错文件过大 最后采取方法二 方法1 网页端导入 可以参考
  • Jquery mobile学习教程之Jquery mobile 二 页面结构

    Jquery Mobile基本框架 在jQuery Mobile中 有一个基本的页面框架模型 就是在页面中通过将一个 标记的 data role 属性设置为 page 形成一个容器或视图 而在这个容器中最直接的子节点应该就是 data ro
  • ftp文件采集

    本地文件根据文件名过滤 import org springframework stereotype Service import java io File import java text ParseException import jav