JAVA處理BLOB類型和CLOB類型的二進制數據

工作的時候遇到了處理二進制流數據的問題,我是用Map<String,Object>進行接收, 獲取value值後,轉成CLOB類型的。通過getSubString方法,讀取它的所有數據,這樣就直接轉換成了String類型的數據。

下面還嘗試過了流的形式處理CLOB的數據(ClobToString方法),但是忘記了爲啥沒選用,似乎是亂碼問題。

CLOB類型數據處理

try {
    List<Map<String, Object>> s = dkDltbMapper.selectAstext(xmGuid, tableName);
    for (Map<String, Object> map : s) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getValue() instanceof oracle.sql.CLOB) {
                try {
                    entry.setValue(convertToStr((oracle.sql.CLOB) entry.getValue()));
                } catch (SQLException e) {
                    //log.error("oracle的 clob類型轉爲string異常:"+e.toString());
                    e.printStackTrace();
                    System.out.println("轉換異常");
                }
            }

        }
    }
    return ResponseResultVo.sucess(s);
} catch (Exception e) {
    return ResponseResultVo.faild(500, e.getMessage());
}



public static String convertToStr(oracle.sql.CLOB clob) throws SQLException {
    return (clob != null ? clob.getSubString(1, (int) clob.length()) : null);

}



public static String ClobToString(Clob clob) throws SQLException, IOException {
    Reader is = clob.getCharacterStream();
    BufferedReader br = new BufferedReader(is);
    String s = br.readLine();
    StringBuffer sb = new StringBuffer();
    while (s != null) {
        sb.append(s);
        s = br.readLine();
    }
    String reString = sb.toString();
    return reString;
}

BLOB類型數據處理
BLOB類型應該也是一樣可以通過getSubString轉換成爲String類型的數據。在研究了下後發現BLOB並沒有此方法
下面是我自己想的,應該是可以的

Blob blob=Blob.class.newInstance();
byte[] bytes = blob.getBytes(1, (int) blob.length());
String s = new String(bytes,"編碼方式");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章