/** * 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; }