mysql blob 读取 图片_mysql中以blob形式存储的图片文件 通过ajax方式传输 在js中设置成img控件的src...
第一步,讀取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;
總結
以上是生活随笔為你收集整理的mysql blob 读取 图片_mysql中以blob形式存储的图片文件 通过ajax方式传输 在js中设置成img控件的src...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人的一生要疯狂一次,无论是为一个人,一段
- 下一篇: Bootstrap第一章初识