Store byte[] in MongoDB using Java -
i insert document collection field of byte[]. when query inserted document field, returns different byte[]. how fix that?
byte[] inputbytes = ... mongocollection<document> collection = _db.getcollection("collectionx"); document doc = new document("test", 1).append("val", inputbytes); collection.insertone(doc.getdocument()); mongocursor<document> result = collection.find(eq("test", 1)).iterator(); document retrived_doc = cursor.next(); cursor.close(); byte[] outputbytes = ((binary)retrived_doc.get("val")).getdata(); // inputbytes = [b@719f369d // outputbytes = [b@7b70cec2
you can encode byte array , store in doc decode after extraction retrieve original.
static string encodebase64string(byte[] binarydata)
encodes binary data using base64 algorithm not chunk output.
static byte[] decodebase64(string base64string)
decodes base64 string octets.
please refer link - https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/base64.html
Comments
Post a Comment