java - get byte array from database -


I want to get a byte array (java) from the database and manipulate it, send it to DOM as the original image In, etc.

  BasicDBObject condition = New BasicDBObject ("_ id", new object id (_id)); DBObject Dataset = Data Entry GetInstanceClass () Getdatasets () FindOne (condition); Byte [] image = (byte []) dataset.get ("image"). ToString () GetBytes (); String s = new string (image); System.out.println ("Provider:" + s);   

It only gives information: Provider: [B @ 3B 240000]

:

  System.out.println (" Provider: "+ image);   

or:

  System.out.println ("Provider:" + new string (image));   

The result of both is to print the default toString () , which is an array of arrays (in your case, rather ugly [ B @ 3B 249009 )

If you want to see more suitable string presentations of the byte array, you can find the answer from:

  System Out.println ("Beautiful string as byte array:" + Arrays.toString (image));   

Also, as you note, you should not (in fact, do not)

  Dataset.Get ("Image") is not required .toString () getBytes () .;   

You are putting your data through all types of mangling.

I have written a short examination to demonstrate how to obtain a byte array outside of Mangaodbi, and it should be used by method:

  @Test There should be a difference in writing as the default string vs using the public should be zeroBabelAetNETNetArringReferenceRepresentationAfabateAre () {// Object ID = new ObjectId; Byte [] bytes = {1,2,3,4}; Collection.insert (new BasicDBObject ("_ id", id) .append ("image", bytes)); // BasicDBObject condition = New BasicDBObject ("_ id", id); DBObject dataset = store.find-on (condition); Byte [] image = (byte []) dataset.get ("image"); // Then // This example is actually meaningless, because it is placed in a byte array above it means that it should be a byte array, but it is to indicate that this is not a string attribute (image example byte [] ); AssertThat (image.toString (), begins ("[b @")); AssertThat (image.toString (). Length (), is (11)); System.out.println ("Byte array to string ():" + image); AssertThat (Arrays.toString (Image), is ("[1, 2, 3, 4]")); System.out.println ("Beautiful String as Byte Array:" + Arrays.toString (Image)); }    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -