A method for JSON-transmitted images in Java

Source: Internet
Author: User
Tags array to string base64

Excerpt from: http://www.cnblogs.com/lilh/p/5352499.html

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 picture to byte array 3 * @return 4 */5 private byte[] LoadImage (file file) {6     ///byte array to return 7     byte[] data=null; 8
    //Open File input stream 9     fileinputstream fin=null;10     //open byte output stream one     bytearrayoutputstream bout=null;12     try{13         //file input stream get corresponding file         fin=new fileinputstream (file);//         output stream definition buffer size:         bout=new Bytearrayoutputstream (int) file.length); n         //define byte array for reading file stream         byte[] buffer=new byte[1024];19         // Used to indicate the location of the read         int len=-1;21         //Start reading the file         ((len=fin.read (buffer))!=-1) {             //starting from the No. 0 position of buffer, Read to the Len position, the result is written to Bout24             Bout.write (buffer,0,len);         }26         //output stream to byte array         data=bout.tobytearray ( )         ///off input/output stream         fin.close ();         bout.close ();     }catch (Exception e) {         E.printstacktrace ();     }34     return data;35}

Then the second step, converting the byte array to a string

1/** 2 * Convert byte array to string----"iso-8859-1" 3 * @param date 4 * @return 5 */6 private string bytetostring (byte[] data) {7     S Tring Datastring=null; 8     try{9         //Convert byte array to string, encoded in format iso-8859-110         datastring=new string (data, "iso-8859-1");     }catch ( Exception e) {         e.printstacktrace ();     }14     return datastring;15}

The next step is to compress the string and return

1/** 2 * Compressed string----"iso-8859-1" 3 * @param data 4 * @return 5 */6 private string compress (string data) {7     string fina Ldata=null; 8     try{9         //open byte output stream         Bytearrayoutputstream bout=new bytearrayoutputstream ()         //Open compressed output stream, The compressed results are placed in bout         gzipoutpustream gout=new gzipoutputstream (bout);         //write to the byte array to be compressed         gout.write ( Data.getbytes ("iso-8859-1"));/         /completion of compression write to         gout.finish (); +         //off output stream at         gout.close ();         Finaldata=bout.tostring ("iso-8859-1");     }catch (Exception e) {         e.printstacktrace ();     }23     Return finaldata;24}

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.