图片经Base64编码后会生成一个特定的字符串,显示时就把这个字符串解码。有两种方式来处理:
1.<img id="img" alt="" src="data:image/gif;base64,${record.bpic}" title="点击看大图">
直接从数据库取出字段数据,放在img标签的src中:src="data:image/gif;base64,${record.bpic}",图片就可以显示了
但是当图片过大时,在IE8下无法查看(IE8只能通过这种方式显示小于32k的图片)
2.<img id="img" alt="" src="${ctx }/build/getImgByte?id=${record.bid}" title="点击看大图">
在后台转码后通过流写出来:
A.
public void getImgByte() {
// 接收施工场地的ID
String bid = getPara("id");
System.out.println(" getImgByte ");
// 数据库读取该条施工场地的数据
Build build = BuildService.service.findFirstByPropertity("bid", bid);
OutputStream os;
try {
os = getResponse().getOutputStream();
// 调用方法将图片内容转成byte并且通过流写出去
GenerateImage(build.getStr("bpic"),os);
} catch (IOException e) {
e.printStackTrace();
}
// 获取的图片信息已经通过流写出去了,因此不再需要跳转
renderNull();
}
B.
public static boolean GenerateImage(String imgStr,OutputStream out) {
if (imgStr == null) // 图像数据为空
return false;
// Base64解码
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}