FileUtils文件名封装处理工具类

该博客介绍了一个Java工具类,用于处理文件路径和后缀名。`FileUtils`类包含两个主要方法:`getFilePath`生成基于当前日期和时间的文件路径,并添加随机数以避免重复;`isFileSuffix`则用于验证文件后缀是否在给定的后缀列表中。此外,博客还引用了`joda-time`和`commons-lang3`库。
摘要由CSDN通过智能技术生成

 

 <dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  </dependency>
 
  <dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  </dependency>
package com.tanhua.common.utils;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;


import java.util.List;

/**
 * 文件名封装处理工具类
 * <dependency>
 * <groupId>joda-time</groupId>
 * <artifactId>joda-time</artifactId>
 * </dependency>
 *
 * <dependency>
 * <groupId>org.apache.commons</groupId>
 * <artifactId>commons-lang3</artifactId>
 * </dependency>
 */
public class FileUtils {
    private FileUtils() {
    }

    /*
     * 测试main 方法。无实际业务应用
     * 生成的文件名: images/2021/07/22/16269213666967476.jpg
     */
    public static void main(String[] args) {
        String filePath = getFilePath("9527.jpg");
        System.out.println(filePath);
    }

    /**
     * 文件新路径,cos桶对象。名,和文件路径。用原始文件名,拼接日期。做路径,当前时间毫秒。做文件名
     * image+年+月+日+时间毫秒+文件名后缀
     *
     * @param sourceFileName 原始文件名
     * @return images/2021/07/22/16269213666967476.jpg
     */
    public static String getFilePath(String sourceFileName) {
        DateTime dateTime = new DateTime();
        return "images/" + dateTime.toString("yyyy")
                + "/" + dateTime.toString("MM") + "/"
                + dateTime.toString("dd") + "/" + System.currentTimeMillis() +
                RandomUtils.nextInt(100, 9999) + "." +
                StringUtils.substringAfterLast(sourceFileName, ".");
    }

    /**
     * 图片做校验,对后缀名 效验, 忽略大小写
     *
     * @param fileNames .jpg .jpeg   List<String>文件后缀集合
     * @param fileName  完整文件名
     * @return 存在返回true 不存在返回false
     */
    public static boolean isFileSuffix(List<String> fileNames, String fileName) {
        for (String name : fileNames) {
            if (StringUtils.endsWithIgnoreCase(fileName, name)) {
                return true;
            }
        }
        return false;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Silence丶你的名字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值