Generally, the picture can be transmitted in the form of a stream, even if it is transmitted by JSON, it is generally an address, and the picture exists on the server, and then send a request to download images along the address.
But in this company's project, the picture is in the Oracle database of the BLOB field, and there is no server, that is, I have to pass the picture itself, and JSON is unable to transfer binary text format, so I want to transfer the picture into a string, Then the other person decodes it and then converts it back on the line.
My method is divided into three steps:
1. Converting a picture to a byte array
2. Converting a byte array to a string
3. Compress strings, put in JSON transfer
The first step is to convert the image to a byte array with the following code:
1 /**2 * Convert a picture to a byte array3 * @return4 */5 Private byte[] LoadImage (file file) {6 //the byte array to return7 byte[] Data=NULL;8 //Open File input stream9FileInputStream fin=NULL;Ten //Open byte output stream OneBytearrayoutputstream bout=NULL; A Try{ - //file input stream get corresponding file -Hi =Newfileinputstream (file); the //output stream definition buffer size -bout=NewBytearrayoutputstream ((int) file.length); - //defines a byte array for reading the file stream - byte[] buffer=New byte[1024]; + //used to indicate the location of the read - intLen=-1; + //Start reading Files A while(Len=fin.read (buffer))!=-1){ at //from the No. 0 position of buffer, read to the Len position, the result is written bout -Bout.write (buffer,0, Len); - } - //Stream the output to a byte array -Data=Bout.tobytearray (); - //turn off the input and output stream in fin.close (); - bout.close (); to}Catch(Exception e) { + e.printstacktrace (); - } the returndata; *}
Then the second step, converting the byte array to a string
1 /**2 * Convert byte array to string----"iso-8859-1"3 * @paramDate4 * @return5 */6 PrivateString bytetostring (byte[] data) {7String datastring=NULL;8 Try{9 //converts a byte array to a string with the encoding format iso-8859-1Tendatastring=NewString (data, "iso-8859-1"); One}Catch(Exception e) { A e.printstacktrace (); - } - returndatastring; the}
The next step is to compress the string and return
1 /**2 * Compress string----"iso-8859-1"3 * @paramData4 * @return5 */6 Privatestring Compress (String data) {7String Finaldata=NULL;8 Try{9 //Open byte output streamTenBytearrayoutputstream bout=NewBytearrayoutputstream (); One //Open the compressed output stream, and the compressed result is placed in the bout AGzipoutpustream gout=Newgzipoutputstream (bout); - //write to a byte array to be compressed -Gout.write (Data.getbytes ("Iso-8859-1")); the //complete the compression write - gout.finish (); - //turn off the output stream - gout.close (); +Finaldata=bout.tostring ("Iso-8859-1"); -}Catch(Exception e) { + e.printstacktrace (); A } at returnFinalData; -}
The above is the method I used, some unfamiliar place accustomed to a sentence a comment is my habit, it may make people look more chaotic, please do not mind, and some comments are my own guess, and not necessarily accurate, if wrong also please point out, thank you!
Then say I then write this method of some trivial things, at the beginning I really want to transfer string transmission, but after all did not write, so also in the online review, Baidu on a lot of said to BASE64 format string transmission, I tried many times, but may be where I write the wrong, leading to no success, My test method is to remove the BLOB field data from the database, and then after the conversion compression, and then generate a local image, but eventually I found that the picture is not open, prompting the file error or corruption, and the image size is a bit strange, it is estimated that I write the wrong code.
Then I simply do not turn Base64, but do not set the encoding format is definitely not, and later in the Aawwmate blog saw a reprint of the article, is also about the JSON transfer pictures, after reading I said in the article in accordance with the ISO-8859-1 format to transmit, indeed succeeded , thank you very much here.
Believe that everyone in the code also saw that the compression string, in fact, the string into a byte array after compression and then into a string bar, why to superfluous, do not directly compress the byte array and then into a string it, in fact, I also thought about it, but I tried or the same old way, Is the local generated pictures can not open, and later have time to try to change to see.
About compression I then tested the next, in fact, the small picture will cause the picture to become larger, at that time as if the test is a 3k of the original picture, after the above three steps, the local decoding unzip the conversion generated by the image is 5k, but I tried a 17k picture, the final picture is 10k, In other words, compression is preferable.
Finally, I would like to say that the above code is hand-punched ... Not directly copied and pasted in the tested code, there may be subtle letter errors--. Please forgive me.
A method for JSON-transmitted images in Java