工作的時候遇到了處理二進制流數據的問題,我是用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,"編碼方式");