Java读取lob格式数据

想要读出lob里面的图片数据,就要确认clob里面存储的是什么,一般情况下存储的base64的串串.所以就以base64为例,而Blob里面存储的大部分是图片数据,但也有xml内容数据.

1查询lob字段内容

Clob clob = rs.getClob(fieldName);
Blob blob = rs.getBlob(fieldName);

2base64解码

    private byte[] getClobData(Clob clob){
        byte[] bt = null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            String base64Str = clob.getSubString(1, (int) clob.length());
            bt = decoder.decodeBuffer(base64Str);
        } catch (Exception e) {
            logger.error("读取clob数据出错", e);
        }
        return bt;
    }
    private byte[] getBlobData(Blob blob) {
        BufferedInputStream is = null;
        byte[] bt = null;
        try {
            is = new BufferedInputStream(blob.getBinaryStream());
            int bufferSize = (int)blob.length();
            bt = new byte[bufferSize];
            try {
                is.read(bt, 0, bufferSize);
            } catch (Exception e) {
                logger.error("读取Blob数据出错", e);
            }
        } catch (Exception e) {
            logger.error("读取Blob字段内容出错", e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
                is = null;
            }
        }
        return bt;
    }

3lob数据保存到本地

    private void saveDataToLocal(byte[] bt){
        try{
            String fileName = "E:/test.jpg";
    //String fileName = "E:/test.xml"
            FileOutputStream out= null;
            out= new FileOutputStream(fileName);
            out.write(bt, 0, bt.length);
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

 

转载于:https://www.cnblogs.com/fxust/p/7459888.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值