package com.biubiu.utils;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Base64;
public class Utils {
/**
* base64转化成图片文件
* @param base64
* @param imgFilePath
* @return
*/
public static boolean generateImage(String base64, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
if (base64 == null) // 图像数据为空
return false;
try {
// Base64解码
byte[] bytes = Base64.getDecoder().decode(base64);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpeg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(bytes);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}