zoukankan      html  css  js  c++  java
  • Blob与Clob转字符串

    /**
         * blob转字符串
         * 
         * @param blob
         * @return
         * @throws IOException
         * @throws SQLException
         */
        public static String blobToString(Blob blob) throws IOException, SQLException {
            InputStream inputStream = blob.getBinaryStream();
            byte[] bytes = null;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bufferBytes = new byte[1024];
            int readLength = 0;
            while ((readLength = inputStream.read(bufferBytes, 0, bufferBytes.length)) > 0) {
                byteArrayOutputStream.write(bufferBytes, 0, readLength);
            }
            bytes = byteArrayOutputStream.toByteArray();
            String str = new String(bytes, "gbk");
            return str;
        }
    
        /**
         * clob转字符串
         * 
         * @param clob
         * @return
         * @throws SQLException
         * @throws IOException
         */
        public static String clobToString(Clob clob) throws SQLException, IOException {
    
            String reString = "";
            Reader is = clob.getCharacterStream();// 得到流
            BufferedReader br = new BufferedReader(is);
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
                sb.append(s);
                s = br.readLine();
            }
            reString = sb.toString();
            return reString;
        }
    
        /**
         * 获取clob或blob字段内容
         * 
         * @param resultSet            结果集
         * @param clobOrBlobColumnName 字段名
         * @return
         * @throws SQLException
         * @throws UnsupportedEncodingException
         * @throws IOException
         */
        public static String getClobOrBlobContent(ResultSet resultSet, String clobOrBlobColumnName)
                throws SQLException, UnsupportedEncodingException, IOException {
            String str = "";
            String type = "";
            for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
                if (resultSet.getMetaData().getColumnName(i).equals(clobOrBlobColumnName)) {
                    type = resultSet.getMetaData().getColumnTypeName(i);
                    break;
                }
            }
            if (type == "BLOB") {
                Blob blob = resultSet.getBlob(clobOrBlobColumnName);
                if (blob != null)
                    str = blobToString(blob);// 获取数据库blob字段
            }
            if (type == "CLOB") {
                Clob clob = resultSet.getClob(clobOrBlobColumnName);
                if (clob != null)
                    str = clobToString(clob);
            }
            return str;
    
        }
  • 相关阅读:
    02 createJS中关于图片优化的问题(推荐一个图片优化的网站)
    01 createJS代码作用域
    文件读取一些payload
    SQL注入笔记-updatexml与extractvalue
    四位数六位数字典生成
    XShell上传文件到Linux服务器上
    centos彻底删除文件夹创建文件
    2020.6.18 时间记录 总学习5.15
    2020.6.14 时间记录
    五种IT职位的介绍
  • 原文地址:https://www.cnblogs.com/ohmyuan/p/10784786.html
Copyright © 2011-2022 走看看