数据库取到blob对象转换成String对象
·
1.用Map查询单条数据。
<select id="" resultType="java.util.Map">
sql语句
</select>
2.将数据强转为blob对象
BLOB blob= (BLOB) map.get("数据列名");
3.将blob对象转成byte[]
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;
}
}
}
4.将byte[] 对象转成String
byte[] result = blobToBytes(blob); String swe=new String(result);
更多推荐
所有评论(0)