Java中blob转base64编码

项目中使用blob字段存储图片,,在页面上展示时,需要进行转化,此处做个笔记:

public static String convertBlobToBase64String(Blob blob) {
String result = "";
if(null != blob) {
try {
InputStream msgContent = blob.getBinaryStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[100];
int n = 0;
while (-1 != (n = msgContent.read(buffer))) {
output.write(buffer, 0, n);
}
result =new BASE64Encoder().encode(output.toByteArray()) ;
output.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();

return result;
}else {
return null;
}
}

你可能感兴趣的:(Java,web)