String与Blob互转 和 file文件与Blob互转

项目中使用大文本,或者是存文件可以使用Blob,这样方便存取,不影响现有的项目代码逻辑

Blob对字符串类型操作

String转换Blob

可以通过SerialBlob创建Blob对象,SerialBlob下有两个构造函数,如需创建Blob对象,可以使用 byte[] 类型,先把String转成byte[]再调用构造函数。
创建SerialBlob对象

String
byte
SerialBlob

String 转换为byte[]

/**
 * String 转 byte[]
 * @param str 请求进入字符串
 * @return 返回byte[] 数组
 * @throws Exception 抛出错误
 */
public byte[] readStream(String str) throws Exception {
    InputStream inStream=new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    return outStream.toByteArray();
}

byte[] 转为 SerialBlob

Blob blob=new SerialBlob(readStream(str))

Blob转换Sting

同理,Blob类型转换为String,就是反过来,先转换成byte[],再转换成String

private String blobToString(Blob blob) throws Exception {
     InputStream inStream=blob.getBinaryStream();
     StringBuffer stringBuffer=new StringBuffer();
     try {
         byte[] buffer = new byte[1024];
         int len = 0;
         while ((len = inStream.read(buffer)) != -1) {
             String str=new String(buffer, 0, len);
             stringBuffer.append(str);
         }
     }catch (Exception e){
         throw e;
     }finally {
         if(inStream!=null){
             inStream.close();
         }
         if(stringBuffer.length()>0){
             return stringBuffer.toString();
         }else{
             return null;
         }
     }
 }

File转Blob

可以把File文件先转换成byte[],再通过SerialBlob对象创建

File转为byte[]

这个方法试试示例,可以去网上找一下File转换byte[]流的方法

//这个方法只是示例,不推荐这样使用
public static byte[] getFileByte(File file) throws Exception {
	byte[] data = null;
	InputStream in = new FileInputStream(file);
	data = new byte[in.available()];
	in.read(data);
	in.close();
	return data;
}

byte[] 转为 SerialBlob

Blob blob=new SerialBlob(readStream(str))

Blob转File

可以通过Blob转成InputStream,再通过读取的方式转成File

Blob
InputStream
File

Blob转InputStream

InputStream in = blob.getBinaryStream()

InputStream转File

public void blobToFile(Blob blob,File file) throws Exception {
	InputStream inStream=null;
	OutputStream outStream=null;
   	try {
		outStream = new FileOutputStream(file);
		int bytesRead = 0;
		byte[] buffer = new byte[2048];
		while ((bytesRead = inStream.read(buffer, 0, 2048)) != -1) {
			outStream.write(buffer, 0, bytesRead);
		}
	}catch (Exception e){
   		throw new Exception(e);
	}finally {
   		if(outStream!=null){
			outStream.close();
		}
		if(inStream!=null){
			inStream.close();
		}
	}
}
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腻&爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值