我無法使用Java 3.4.2驅動程序從MongoDB讀取blob(二進制)記錄。使用Java從mongodb讀取blob(二進制)數據時出錯
BasicDBObject whereClause = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
obj.add(new BasicDBObject("blobcontentid", "20160601201035069394000000"));
whereClause.put("$and", obj);
MongoCursor<Document> cursor = contentcollection.find(whereClause).iterator();
while (cursor.hasNext()) {
Document object = cursor.next();
System.out.println(object.getString("blobcontentid"));
if (object.get("content") != null){
byte[] content = (byte []) object.get("content");
} else {
System.out.println("Content is empty");
}
}
錯誤:java.lang.ClassCastException:org.bson.types.Binary不能轉換爲[B
相同的記錄我在DB2閱讀這樣。 byte [] content = aResult.getBytes(「CONTENT」);
預先感謝您! Bharathi
實際上有一個['.getData()'](http://api.mongodb.com/java/current/org/bson/types/Binary.html#getData--)方法定義在'Binary '類型。哪一個當然會返回'byte []',所以不需要強制執行這樣的強制轉換。只需使用正確的方法。 –