base64转图片

/**
*
* @param base64Str
* @param path
* @return
*/
public static File base642File(String base64Str, String path) {
if (!StringUtils.isNotBlank(base64Str)) {
return null;
}
if (!StringUtils.isNotBlank(path)) {
return null;
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = new byte[0];
try {
b = decoder.decodeBuffer(base64Str);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(path);
out.write(b);
out.flush();
out.close();
log.info("根据base64字符串 生成图片成功。");
return new File(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}
posted @ 2021-11-16 10:41  游走人间的蒲公英  阅读(122)  评论(0编辑  收藏  举报