common tools java development

tools-common

Providing verification processing parameters, excel export, time conversion, data encryption, authentication code, send messages and so on ...

We recommend using the latest version

installation

<dependency>
  <groupId>cn.gjing</groupId>
  <artifactId>tools-common</artifactId>
  <version>1.0.2</version>
</dependency>

Use


annotation:

* @NotNull: 可以在普通程序和web程序中使用,适用于方法参数校验,如若要排除方法中的某个参数,搭配使用@ExcludeParam注解到指定参数上;
* @NotNull2: 只在web程序中使用,适用于方法,如若要排除方法中的某个参数不检验,可进行@NotNull(exclude={"参数名1","参数名2"}),**参数名必须与方法的参数名相同**,   
                         默认异常信息为参数不能为空,可以自定义异常信息@NotNull(message="您要使用的异常异常");   
* @EnableCors: 允许跨域,标注在启动类上

Return result template:

  • ResultVo: General return result template comprising code (status code), Message (message), Data (data) three parameters
  • PageResult: query results page template includes data (data) and TotalPage (total number of pages) and CurrentPage (current page) can be used directly configured using builder, which may be of the method call.
  • ErrorResult: error return template, which contains failure (using the status code 400, and a Message contains code, code for determining a further error), error (abnormality-server, 500, etc. is generally used, comprising only the message)

Excel:

  • Export: response, headers, title can not be empty
@RequestMapping("/excel")
public void excel(HttpServletResponse response) {
    String[] headers = {"标题1", "标题2"};
    ExcelUtil.excelExport(response, null, headers, "测试无内容excel", null);
}

@RequestMapping("/excel2")
public void excelContainsValue(HttpServletResponse response) {
    String[] headers = {"标题1", "标题2"};
    List<Object[]> data = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Object[] objects = new Object[headers.length];
        objects[0] = i;
        objects[1] = i+1;
        data.add(objects);
    }
    ExcelUtil.excelExport(response, data, headers, "测试含有内容excel", null);
}

@RequestMapping("/excel3")
public void excelContainsInfo(HttpServletResponse response) {
    String[] headers = {"标题1", "标题2"};
    List<Object[]> data = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Object[] objects = new Object[headers.length];
        objects[0] = i;
        objects[1] = i+1;
        data.add(objects);
    }
    ExcelUtil.excelExport(response, data, headers, "测试含有详情的excel", "详情");
}

Utility class:

  • ParamUtil: Main parameters providing verification, processing, matching the like;
里面主要包括(方法名:对应功能): 
    1.isEmpty: 判断给定参数是否为空; 2.isNotEmpty: 不为空; 3.listHasEmpty: 判断集合里是否含空; 4.requireNotNull: 不为空返回原值;   
    5.multiEmpty: 多参数判断是否含空; 6.equals: 判断两个参数是否相等; 7.trim: 去除参数或者集合里每个参数的多余的空;   
    8.removeSymbol: 移除字符串两边的符号,对应的startSymbol和endSymbol为首尾坐标; 9.removeSymbol2:移除参数中所有的符号;   
    10.split: 根据符号分割; 11.contains: 判断数组里是否含某值; 13.其他手机号,邮箱等匹配.
  • TimeUtil: mainly used for operating time and date;
里面包括(方法名:对应功能):
    1.dateToString: 获取文本时间; 2.stringToDate:获取Date; 3.stringDateToCalendar: 文本时间转成日期; 4.calendarToDate:日期转Date; 5.calendarToStringDate: 日期转文本;   
    6.getAllDaysOfMonth: 获取某个日期的当月总天数; 7.getDays: 获取给定日期的天数; 8.getYears:获取给定日期的年份; 9:getMonth:获取给定日期的月份; 10.addMonth:给定日期增加月份.   
    11.addDay:日期上增加天数; 12.stringDateToStamp: 文本时间转为时间戳; 13.stampToStringDate:时间戳转为文本时间; 14: dateBetween:获取两个日期相差的天数,带include的为包括今天;   15.dateToLocalDateTime:Date转LocalDateTime; 16.dateToLocalDate:Date转LocalDate; 17. localDateToDate: LocalDate转Date; 18.localDateToString: localDate转String;   
    19: stringToLocalDate: String格式日期转LocalDate; 20: localDateTimeToStamp: localDateTime转时间戳; 21: getYearsByStartTime: 查询指定日期距离当前多少年;   
    22: stampToLocalDateTime: 时间戳转LocalDateTime.......
  • EncryptionUtil: mainly used for encryption, it now contains MD5, sha256Hmac, sha1Hmac, base64;
  • AuthCodeUtil: simple authentication code tools, currently only supports English and digital hybrid code, the latter will add puzzles and other types of code;
//第一种情况: 生成验证码到本地指定路径,以下为简单测试,具体逻辑根据业务需求自行设计
public static void main(String[] args) {
    AuthCodeUtil authCodeUtil = new AuthCodeUtil(160,40,5,150);
    try {
        String path="/文件夹路径/code.png";
        //写入到本地时可以通过getCode()方法获取生成的验证码
        String code = authCodeUtil.writeToLocal(path).getCode();
        System.out.println(code);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
//第二种情况: 以流的方式返回给前端
@GetMapping("/code")
public void getCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
    AuthCodeUtil authCodeUtil = new AuthCodeUtil(100, 50, 4, 50);
    response.setContentType("image/jpeg");
    //禁止图像缓存
    response.setHeader("param", "no-cache");
    response.setDateHeader("Expires", 0);
    //该案例将验证码存在session中,具体业务中根据场景自行设计
    HttpSession session = request.getSession();
    session.setAttribute("code", authCodeUtil.getCode());
    authCodeUtil.write(response.getOutputStream());
}

/**
 * 验证码验证,由于示例为服务端session存储,所以下面为session方式验证,具体根据个人存储条件相应更改
 * @param code 前端传来的验证码
 * @param request request
 * @return .
 */
@PostMapping("/verifyCode")
@NotNull2(exclude = {"request"})
public ResultVo verifyCode(String code, HttpServletRequest request) {
    HttpSession httpSession = request.getSession();
    String sessionCode = (String) httpSession.getAttribute("code");
    if (ParamUtil.isEmpty(sessionCode)) {
        return ResultVo.error(null, "code不存在");
    }
    if (sessionCode.toLowerCase().equals(code.toLowerCase())) {
        return ResultVo.success();
    }
    return ResultVo.error(null,"invalid code");
}
  • EmailUtil: e-mail tools, support and regular mail messages with attachments, supports html format text, and support for bulk carbon copy, send returns true for success
里面参数主要包括: host(smtp服务器地址,比如qq邮箱:smtp.qq.com);password(发送者邮箱密码,有些邮箱需要用授权码代替密码);from(发送人邮箱地址);subject(邮件主题);body(邮件内容,支持html);   
files(要发送的附件物理地址,不要可以传null或者空数组);tos(收件人邮箱账号,多个逗号隔开);copyTo(抄送人邮箱地址,多个逗号隔开,不抄送可以传null或者空字符串"");
案例:
public static void main(String[] args) {
    boolean b = EmailUtil.of("smtp.qq.com", "发送人密码或者授权码", "发送人邮箱")
            .sendEmail("主题", "内容",new String[]{"附件物理地址"},"收件人邮箱地址", "抄送人邮箱地址");
    if (b) {
        System.out.println("发送成功");
    }
}

Guess you like

Origin yq.aliyun.com/articles/704350