Java-开发常用工具集

Lombok

  • 依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    </dependency>
  • 注解

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    @Data
    @ToString
    @AllArgsConstructor // 全参构造
    @NoArgsConstructor // 无参构造
    public class User {
    private String userName;
    private Integer age;
    }
    // @Data: 生成如下方法
    See Also:
    Getter,
    Setter,
    RequiredArgsConstructor,
    ToString,
    EqualsAndHashCode,
    Value
  • 原理

    原理
  • 注解解析

    • @Data

    • @Accessors

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      @Data
      // 开启链式调用
      @Accessors(chain = true)
      public class User {
      private String userName;
      private Integer age;
      }


      // 链式调用实例
      public class UserDao {
      public static void main(String[] args) {
      User user = new User();
      user.setUserName("coder-itl")
      .setAge(18);
      }
      }

Hutool

  • 包含组件

    一个Java基础工具类,对文件、流、加密解密、转码、正则、线程、XMLJDK方法进行封装,组成各种Util工具类,同时提供以下组件:

    模块 介绍
    hutool-aop JDK动态代理封装,提供非IOC下的切面支持
    hutool-bloomFilter 布隆过滤,提供一些Hash算法的布隆过滤
    hutool-cache 简单缓存实现
    hutool-core 核心,包括Bean操作、日期、各种Util
    hutool-cron 定时任务模块,提供类Crontab表达式的定时任务
    hutool-crypto 加密解密模块,提供对称、非对称和摘要算法封装
    hutool-db JDBC封装后的数据操作,基于ActiveRecord思想
    hutool-dfa 基于DFA模型的多关键字查找
    hutool-extra 扩展模块,对第三方封装(模板引擎、邮件、Servlet、二维码、Emoji、FTP、分词等)
    hutool-http 基于HttpUrlConnectionHttp客户端封装
    hutool-log 自动识别日志实现的日志门面
    hutool-script 脚本执行封装,例如Javascript
    hutool-setting 功能更强大的Setting配置文件和Properties封装
    hutool-system 系统参数调用封装(JVM信息等)
    hutool-json JSON实现
    hutool-captcha 图片验证码实现
    hutool-poi 针对POIExcelWord的封装
    hutool-socket 基于JavaNIOAIOSocket封装
    hutool-jwt JSON Web Token (JWT)封装实现
  • 依赖

    1
    2
    3
    4
    5
    6
    7
    <dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.5</version>
    </dependency>


  • Convert

    1
    import cn.hutool.core.convert.Convert;
    • 转换为字符串

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      @Test
      void testConvertToStr() {
      // 数字转换为字符串
      String toStr = Convert.toStr(1);
      System.out.println("toStr = " + toStr); // toStr = 1
      // 数组转化为字符串
      long[] array = {1,2,3,4,5};
      String arrayToStr = Convert.toStr(array);
      System.out.println("arrayToStr = " + arrayToStr); // arrayToStr = [1, 2, 3, 4, 5]
      }
    • 转换为指定类型数组

      1
      2
      3
      4
      5
      6
      7
      8
      9
      @Test
      void testConvertArrayToOtherType() {
      // 将数组转换为其他指定类型
      long[] array = {1,2,3,4,5};
      Integer[] integersArray = Convert.toIntArray(array);
      for (Integer item : integersArray) {
      System.out.println(item);
      }
      }
    • 转换为日期对象

      1
      2
      3
      4
      5
      6
      7
      8
      9
      @Test
      void testConvertDate() throws ParseException {
      String dateInfo = "2022-12-01";
      Date parseDateInfo = new SimpleDateFormat("yyyy-MM-dd").parse(dateInfo);
      System.out.println("parseDateInfo = " + parseDateInfo);
      // 将字符串转换为日期格式
      Date date = Convert.toDate(dateInfo);
      System.out.println(date);
      }
    • 数组转集合

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      @Test
      void testConvertArrayToList() throws ParseException {
      // 将数组转换为集合
      long[] array = {1, 2, 3, 4, 5};
      // 1. asList
      List<long[]> longs = Arrays.asList(array);
      // 2. Convert.
      List<Long> objects = (List<Long>) Convert.toList(array);
      objects.forEach(s -> System.out.println(s));
      }
  • IO

    • 文件拷贝

      • 原始方法

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        @Test
        void testIO() throws IOException {
        FileInputStream is = new FileInputStream(new File("E:\\springboot-demo\\springboot-junit\\src\\main\\resources\\uploadFile\\init.md"));
        FileOutputStream os = new FileOutputStream(new File("E:\\springboot-demo\\springboot-junit\\src\\main\\resources\\uploadFile\\init3.md"));
        int len = 0;
        while (true) {
        // 定义缓冲区
        byte[] bytes = new byte[1024];
        len = is.read(bytes);
        if (len == -1) break;
        os.write(bytes, 0, len);
        }
        is.close();
        os.close();
        }
      • 工具类

        1
        2
        3
        4
        5
        6
        7
        8

        @Test
        void testIO() throws FileNotFoundException {
        FileInputStream is = new FileInputStream(new File("E:\\springboot-demo\\springboot-junit\\src\\main\\resources\\uploadFile\\init.md"));
        FileOutputStream os = new FileOutputStream(new File("E:\\springboot-demo\\springboot-junit\\src\\main\\resources\\uploadFile\\init2.md"));
        // 一行语句
        IoUtil.copy(is, os);
        }
  • md5

    1
    2
    3
    4
    5
    @Test
    void testMD5() {
    String md5 = SecureUtil.md5("123456");
    System.out.println("md5 = " + md5);
    }
  • jWT

    • JWT 创建

      1
      2
      3
      4
      5
      6
      7
      8
      9
      Map<String, Object> map = new HashMap<String, Object>() {
      private static final long serialVersionUID = 1L;
      {
      put("uid", Integer.parseInt("123"));
      put("expire_time", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 15);
      }
      };

      JWTUtil.createToken(map, "1234".getBytes());
    • JWT 解析

      1
      2
      3
      4
      5
      6
      7
      8
      String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
      "eyJzdWIiOiIxMjM0NTY3ODkwIiwiYWRtaW4iOnRydWUsIm5hbWUiOiJsb29seSJ9." +
      "U2aQkC2THYV9L0fTN-yBBI7gmo5xhmvMhATtu8v0zEA";

      final JWT jwt = JWTUtil.parseToken(rightToken);

      jwt.getHeader(JWTHeader.TYPE);
      jwt.getPayload("sub");
    • JWT 验证

      1
      2
      3
      4
      5
      String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
      "eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFsbCJdLCJleHAiOjE2MjQwMDQ4MjIsInVzZXJJZCI6MSwiYXV0aG9yaXRpZXMiOlsiUk9MRV_op5LoibLkuozlj7ciLCJzeXNfbWVudV8xIiwiUk9MRV_op5LoibLkuIDlj7ciLCJzeXNfbWVudV8yIl0sImp0aSI6ImQ0YzVlYjgwLTA5ZTctNGU0ZC1hZTg3LTVkNGI5M2FhNmFiNiIsImNsaWVudF9pZCI6ImhhbmR5LXNob3AifQ." +
      "aixF1eKlAKS_k3ynFnStE7-IRGiD5YaqznvK2xEjBew";

      JWTUtil.verify(token, "123456".getBytes());
  • 图形验证码

    https://www.hutool.cn/docs/#/captcha/%E6%A6%82%E8%BF%B0?id=linecaptcha-%e7%ba%bf%e6%ae%b5%e5%b9%b2%e6%89%b0%e7%9a%84%e9%aa%8c%e8%af%81%e7%a0%81