java 从数据库取出文件流转换成字符串BlobToBase64String
从数据库里面取出数据
//照片
sb.setLength(0);
sb.append("SELECT grzp,rybm from GG_ZGZP where rybm = '"+RenYuanBianMa+"'");
ResultSet rs;
String zpStr = "";
if(st != null){
rs = st.executeQuery(sb.toString());
if (rs.next()) {
Blob blob = rs.getBlob(1);
zpStr = this.convertBlobToBase64String(blob);
}
}
转换函数
private 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;
}
end