第一步,读取blob数据,

第二步,将blob数据转换成byte数组

第三步,将byte数据进行base64加密转换成字符串并回传

第四步,接收字符串

第五步,将img控件的src设置成"data:image/jpeg;base64,"+接收的字符串;

相关代码:

java:

public String getAccountImg(String alias)

{

String sql = "SELECT imgrawdata FROM wx_account WHERE alias = ?";

PreparedStatement ps = DBUtils.createPreparedStatement(DBUtils.connection, sql);

DBUtils.setString(ps, 1, alias);

ResultSet res = DBUtils.executeQuery(ps);

DBUtils.next(res);

Blob imagerawdata = DBUtils.getBlob(res, "imgrawdata");

byte[] b = blobToBytes(imagerawdata);

return Base64.encode(b);

}

private byte[] blobToBytes(Blob blob) {

BufferedInputStream is = null;

try {

is = new BufferedInputStream(blob.getBinaryStream());

byte[] bytes = new byte[(int) blob.length()];

int len = bytes.length;

int offset = 0;

int read = 0;

while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {

offset += read;

}

return bytes;

} catch (Exception e) {

return null;

} finally {

try {

is.close();

is = null;

} catch (IOException e) {

return null;

}

}

}

~~~~~~~~~~~~~~

js:

var img= xmlhttp.responseText;

alert(img);

document.getElementById("image").src = "data:image/jpeg;base64,"+img;

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐