hutool 常用工具类 ---(有空看看-含验证码)前端流文件生成图片展示的方法
https://www.hutool.cn/docs/#/
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.1</version>
</dependency>
验证码
后端代码
@PostMapping("/no2")
//获取用户信息的方法 用Authentication类
public void NO2(HttpServletResponse response) throws IOException {
// HttpHeaders headers = new HttpHeaders();
//// //生成验证码图片
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
//输出code,这里可以存到redis,前端给个随机码 +这个验证码
Console.log("输出验证码code: "+lineCaptcha.getCode());
//指定响应头给浏览器
response.setContentType("image/png");//告诉浏览器输出内容为图片
response.setHeader("Pragma", "No-cache");//禁止浏览器缓存
response.setHeader("Cache-Control", "no-cache");
//输出流文件给前端
lineCaptcha.write(response.getOutputStream());
}
前端代码
<template>
<div>
<img :src="src" alt="图像的替代文本">
<button @click="post">更新验证码</button>
</div>
</template>
<script>
import {photo} from "../../api/index";
import {photo2} from "../../api/index";
export default {
data() {
return {
type:'1',
src:'https://emoji.cdn.bcebos.com/yunque/hejirukou.jpg',
}
},
methods: {
//下载验证码
async post(){
try {
let { response } = await photo();
const objectUrl = window.URL.createObjectURL(new Blob([response], {type: "image/png"}));
this.src = objectUrl; //到这步已经实现图片展示了
const a = document.createElement('a'); //模拟生成一个a标签 点就下载
a.href = objectUrl;
a.download = '2023.png';
a.click();
a.remove();
} catch (e) {
console.log(e);
}
}
}}
</script>
<style>
</style>
常用api
//把one实体类 复制到 userDTO 实体类 ,相同字段会自动赋值过去
import cn.hutool.core.bean.BeanUtil;
BeanUtil.copyProperties(one, userDTO, true);
//判断变量是否为空
import cn.hutool.core.util.StrUtil;
StrUtil.isBlank(username)
import cn.hutool.core.date.DateUtil;
//当前时间加20秒后的时间
DateUtil.offsetSecond(new Date(), 20)
当前时间加2小时后的时间
DateUtil.offsetHour(new Date(), 2)
//输出结果: 2022-06-14 17:28:26
//格式化
import cn.hutool.core.util.StrUtil;
String template = "{}爱{},就像老鼠爱大米";
String str = StrUtil.format(template, "我", "你"); //str -> 我爱你,就像老鼠爱大米
//字符转json
import cn.hutool.json.JSONUtil;
String html = "{\"name\":\"Something must have been changed since you leave\"}";
JSONObject jsonObject = JSONUtil.parseObj(html);
jsonObject.getStr("name");
System.out.println(jsonObject);
//List转Json,maps是List类型的参数
String json = JSONUtil.toJsonStr(maps);
System.out.println("这是json字符串: "+json);
//Json转List
//例子: {
// "items": [
// {
// "id": "62f64658923314000118f4f6",
// "phoneNumber": "15994727178",
// "captcha": "247743",
// "state": "SUCCESS",
// "sendTime": 1660307032
// }]
// }
JSONArray objects =JSONUtil.parseArray(json);
List<Map> maps1 = JSONUtil.toList(objects, Map.class);
System.out.println("这是list集合: "+maps1);
String ss="{'items': [{'id':'62f64658923314000118f4f6'}]}";
JSONObject jsonObject = JSONUtil.parseObj(ss);
System.out.println(jsonObject); //{"items":[{"id":"62f64658923314000118f4f6"}]}
List<Map> maps1 = JSONUtil.toList(jsonObject.getJSONArray("items"), Map.class);
System.out.println("这是list集合: "+maps1); //这是list集合: [{id=62f64658923314000118f4f6}]--类型 java.util.ArrayList
json转java对象
场景: 接收前端100多个字段的json,直接映射到实体类里面,然后通过Mongo传实体类分别把字段插入到数据库
AcceptCancelationApplyReq req = JSONUtil.toBean(request, AcceptCancelationApplyReq.class);