概要:
Blob类型:是一个二进制类型,常用于存储大文本,图像 等等二进制数据,
在网页开发中我们经常遇到这样的需求,需将富文本字符串转换为Blob数据类型,
然后存储到运算服务器上,那么java中如何将富文本转换为Blob数据类型呢?
例子:
1.对应的实体类封装:
/**
* 界址点
* 存储BLOB类型数据
*/
@Column(name = "SHAPE" )
private byte[] shape;
2.从数据库获取相应值,将Blob转换成String:
if(gddk.getShape()!=null){
//BLOB字段转字符串
String shape = new String(gddk.getShape(),"UTF-8");
}
3. 将Blob转化为byte[],可以转化二进制流的:
/**
* 将blob转化为byte[],可以转化二进制流的
*
* @param blob
* @return
*/
public static byte[] blobToBytes(Blob blob) {
InputStream is = null;
byte[] b = null;
try {
is = blob.getBinaryStream();
b = new byte[(int) blob.length()];
is.read(b);
return b;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
is = null;
} catch (IOException e) {
e.printStackTrace();
}
}
return b;
}