ftp文件采集

本地文件根据文件名过滤




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 "";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值