前台BASE64编码方式显示图片;
<img src="https://img-blog.csdnimg.cn/2022010704515830585.jpg"+BASE64code id="idPic"/>
后台将BASE64字符串解析为图片:
public static boolean GenerateImage(String imgStr,String imgFilePath ){
if (imgStr == null)
return false;
BASE64Decoder decoder = new BASE64Decoder();
try{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i){
if(b[i]<0) {
b[i]+=256;
}
}
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}catch (Exception e) {
return false;
}
}
PS:BASE64Decoder为jdk自带jar,若显示不存在,移除jdk再重新添加即可显示。