- 前端处理
axios({
url: \’/development-web/img/code\’,
method: \’post\’,
responseType: \’arraybuffer\’
}).then(res => {
let bytes = new Uint8Array(res.data);
let data = \”\”;
let len =
.byteLength;
for (let i = 0; i < len; i++) {
data += String.fromCharCode(bytes[i]);
}
this.srcKaptcha = \”data:image/png;base64,\” + window.btoa(data);
})
注意responseType的类型是一个数组,
后台直接用response.out输出;
- 后端处理
// 生成文字验证码
String text = producer.createText();
// 生成图片验证码
ByteArrayOutputStream outputStream = null;
BufferedImage image = producer.createImage(text);
outputStream = new ByteArrayOutputStream();
ImageIO.write(image, \”jpg\”, outputStream);
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
******转化为base64字节输出
ncoder.encode(outputStream.toByteArray())
前台AXIOS
dataType: \”json\”, 接受类型改成json
vm.src = \’data:image/jpeg;base64,\’+result.img;
加上类型编码的前缀即可