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);