获取指定文件夹下的文件,不包含文件夹,拿到文件创建时间,把文件转成base64

/**
     *  <h1>获取指定文件夹下所有文件,不含文件夹</h1>
     * @param dirFile 文件夹
     * @return
     */
    public static List<File> getAllFile(File dirFile){
        // 如果文件夹不存在或着不是文件夹,则返回 null
        if(Objects.isNull(dirFile) || !dirFile.exists() || dirFile.isFile()) {
            return null;
        }

        File[] childrenFiles =  dirFile.listFiles();
        if(Objects.isNull(childrenFiles) || childrenFiles.length == 0) {
            return null;
        }

        List<File> files = new ArrayList<>();
        for(File childFile : childrenFiles) {

            // 如果时文件,直接添加到结果集合
            if(childFile.isFile()) {
                files.add(childFile);
            }else {
                // 如果是文件夹,则将其内部文件添加进结果集合
                List<File> cFiles =  getAllFile(childFile);
                if(Objects.isNull(cFiles) || cFiles.isEmpty()) {
                    continue;
                }
                files.addAll(cFiles);
            }

        }

通过File的getPath()方法 可以拿到路径

 public static void main(String[] args) {
        String property = System.getProperty("user.dir");
        String filePath = property + "/wordtemplate/childrenMedicalTemplate/22";
        List<File> allFile = getAllFile(new File(filePath));
   for (File file : allFile) {
            String parent = file.getPath();
            try {
                //文件传base64
                String base64 = fileToBase64(file);
                //拿到文件创建时间
                Instant instant = Files.readAttributes(Paths.get(parent), BasicFileAttributes.class).creationTime().toInstant();
                //格式化时间
                ZonedDateTime littleDate = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                //格式化后的创建时间
                String createdDate = littleDate.format(dateTimeFormatter);

                System.out.println(base64);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

/**
     * 文件File类型转byte[]
     *
     * @param file
     * @return
     */
    public static byte[] fileToByte(File file) {
        byte[] fileBytes = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            fileBytes = new byte[(int) file.length()];
            fis.read(fileBytes);
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileBytes;
    }

    /**
     * 文件File类型转BASE64
     *
     * @param file
     * @return
     */
    public static String fileToBase64(File file) {
        return Base64.getEncoder().encodeToString(fileToByte(file));
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值