前台:
base64的图片可以直接显示在网页上面
<img src=“data:image/png;base64,***************************************************"/>
后台:
将图片文件转化为字节数组字符串,并对其进行Base64编码处理
org.apache.tomcat.util.codec.binary.Base64包下
调用.encodeBase64() 方法,传入字节数组
protected String ImageToBase64(InputStream is) {
byte[] data = null;
// 读取图片字节数组
try {
data = new byte[is.available()];
is.read(data);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
// 返回Base64编码过的字节数组字符串
return new String(Base64.encodeBase64(data));
}