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