摘要:
下文讲述Java中Blob类型同String类型之间相互转换的方法说明,如下所示;
Blob类型:是一个二进制类型,常用于存储大文本,图像 等等二进制数据,
在网页开发中我们经常遇到这样的需求,需将富文本字符串转换为Blob数据类型,
然后存储到运算服务器上,那么java中如何将富文本转换为Blob数据类型呢?
下文将一一道来,如下所示:
实现思路:
字符串转Blob
new SerialBlob("字符串信息".toCharArray());
Blob转字符串
new String("blob值".getBytes(1, (int)"blob值".length()),"GBK")
例:
java中字符串和Blob之间相互转换的示例分享
String s="猫猫教程-树大招风";
//String 转 blob
Blob b = new SerialBlob(s.getBytes("GBK"));
//或不传入字符集,使用系统默认
Blob b = new SerialBlob(s.getBytes());
//blob转字符串
String blobString = new String(b.getBytes(1, (int) b.length()),"GBK");
//或
String blobString = new String(b.getBytes(1, (int) b.length()));
//输出blob转换后的字符串信息
System.out.println(blobString);