zoukankan      html  css  js  c++  java
  • 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();
            }
        }
  • 相关阅读:
    仿12306客户端
    object-c开发中混合使用或不使用ARC
    Objective-c 的 @property 详解
    iPhone的Push(推送通知)功能原理浅析
    Objective-C内存管理教程和原理剖析3
    IDEA 创建JAVA Maven Web 工程
    Linux CenOS 7 安装Redis
    Linux CenOS 7 安装Tomcat
    Linux CentOS 7 安装wordpress
    Linux CenOS 7 安装JDK
  • 原文地址:https://www.cnblogs.com/fxust/p/7459888.html
Copyright © 2011-2022 走看看