In java, the method for transmitting images in json format is javajson.

Source: Internet
Author: User
Tags array to string

In java, the method for transmitting images in json format is javajson.

Generally, an image can be transmitted in the form of a stream. Even if it is transmitted in json format, an address is usually transmitted, and the image is stored on the server and then sent along the address to download the image.

However, in this project, images are stored in the blob field in the oracle database and are not stored on the server. That is to say, I must pass the image itself, json cannot transmit binary text format, so I want to convert the image into a string for transmission, and then the other party decodes the image and converts it back.

My methods are divided into three steps:

1. Convert the image into a byte array

2. Convert the byte array into a string

3. compress the string and put it in json for transmission.

The first step is to convert the image into a byte array. The Code is as follows:

1/** 2 * convert the image to a byte array 3 * @ return 4 */5 private byte [] loadImage (File file File) {6 // The returned byte array 7 byte [] data = null; 8 // open the file input stream 9 FileInputStream fin = null; 10 // open the byte output stream 11 ByteArrayOutputStream bout = null; 12 try {13 // obtain the corresponding file 14 fin = new FileInputStream (file) from the file input stream ); 15 // The output stream defines the buffer size 16 bout = new ByteArrayOutputStream (int) file. length); 17 // defines the byte array for reading the file stream 18 byte [] buffer = new byte [1024]; 19 // indicates the read position 20 int len =-1; 21 // Start reading the file 22 while (len = fin. read (buffer ))! =-1) {23 // read to the len position starting from the buffer's 0th position and write the result to bout24 bout. write (buffer, 0, len); 25} 26 // transfers the output to the byte array 27 data = bout. toByteArray (); 28 // close the input/output stream 29 fin. close (); 30 bout. close (); 31} catch (Exception e) {32 e. printStackTrace (); 33} 34 return data; 35}

The second step is to convert the byte array into 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 String dataString = null; 8 try {9 // convert the byte array into a String encoded in the format of ISO-8859-110 dataString = new String (data, "ISO-8859-1 "); 11} catch (Exception e) {12 e. printStackTrace (); 13} 14 return dataString; 15}

The next step is the last step. 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 finalData = null; 8 try {9 // open the byte output stream 10 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 11 // open the output stream for compression, the compressed result is placed in bout 12 GZIPOutpuStream gout = new GZIPOutputStream (bout); 13 // write the byte array to be compressed 14 gout. write (data. getBytes ("ISO-8859-1"); 15 // complete the compressed write 16 gout. finish (); 17 // close the output stream 18 gout. close (); 19 finalData = bout. toString ("ISO-8859-1"); 20} catch (Exception e) {21 e. printStackTrace (); 22} 23 return finalData; 24}

The above is the method I used. I am used to comments a sentence in some unfamiliar places. It may make people look messy. Don't mind, in addition, some comments are self-guessed and not necessarily accurate. If you are wrong, please point them out. Thank you!

Then let's talk about some trivial things I wrote about this method at the time. At the time, I did want to transfer strings, but I did not write it, so I also checked it online, many Baidu users refer to converting strings to Base64 format for transmission. I tried it many times. However, it may be because I did not write the correct information, which has never been successful, my test method is to retrieve the data of the blob field from the database, convert and compress the data, and generate an image in the mirror, but I finally found that the image cannot be opened, the system prompts that the file is wrong or damaged, and the image size is a little weird. I guess the code is wrong.

Then I simply did not convert it to Base64, but it is definitely not feasible to do not set the encoding format. Later, I saw a reprinted article in The aawwmate blog about the transmission of pictures in json format, after reading, I will follow the article said in the ISO-8859-1 format to transfer, it is indeed successful, thank you very much here.

I believe that when we see the compressed string in the code, we actually convert the string into a byte array and compress it into a string, I did not directly compress the byte array before converting it into a string. In this regard, I also thought about it, but I tried it again, but the image generated locally cannot be opened, try again later.

For compression, I tested it at the time. In fact, small images will lead to larger images. At that time, it seems that a 3 K original image was tested. After the above three steps, the locally decoded, decompressed, and converted image is 5 K, but I tried a 17 K image. The final generated image is 10 K, which means compression is desirable.

Finally, I want to say that the above Code is hand-coded ...... It is not directly copied and pasted in the tested Code. There may be slight letter errors --. Sorry.

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.