web123456

java json to binary data_Json Binary Data

json is a very simple protocol, but unfortunately, it can only pass basic number types (int, long, string, etc.), but not byte types. If you want to transfer binary files such as images, there is no way to transfer them directly.

This article provides a way of thinking for your reference, so that you can transfer binary files in json, if you have this need and do not know how to realize it, perhaps this article can help you. The idea applies to all languages, this article to java implementation, I believe we can easily be transformed into their own know how language.

reasoning

1. Reading binary files into memory

2. Compress it with Gzip. After all, it's a network transfer, but you can leave it uncompressed.

3. UseBase64 Convert byte[] to string

Addendum: What is Base64

The following is an excerpt from Nguyen Yifeng's blog,Base64 specific encoding, you can go directly to.

Base64 is an encoding that converts 8-bit non-English characters into 7-bit ASCII characters. This was originally intended to fulfill the rule that non-ASCII characters could not be used directly in e-mail, but it has other important implications as well:

a) All binary files can, as a result, be converted to printable text code and edited using text software;

b) Ability to perform simple encryption of text.

realization

The main idea is that the above 3 steps, add the string to the json field sent to the server, and then the server then Base64 decryption -> Gzip decompression, you can get the original binary file. Is not it very simple? Said a lot, here we come to look at the specific code implementation.

*** Note: Java SE is no way to directly use Base64 Oh, you must first download a copy of their own. But Android has integrated Base64, so you can use it directly in Android.

/**

* @author xing

*/

public class TestBase64 {

public static void main(String[] args) {

byte[] data = compress(loadFile());

String json = new String(Base64.encodeBase64(data));

("data length:" + ());

}

/**

* :: Load local files and convert to byte arrays

* @return

*/

public static byte[] loadFile() {

File file = new File("d:/");

FileInputStream fis = null;

ByteArrayOutputStream baos = null;

byte[] data = null ;

try {

fis = new FileInputStream(file);

baos = new ByteArrayOutputStream((int) ());

byte[] buffer = new byte[1024];

int len = -1;

while ((len = (buffer)) != -1) {

(buffer, 0, len);

}

data = () ;

} catch (IOException e) {

();

} finally {

try {

if (fis != null) {

();

fis = null;

}

() ;

} catch (IOException e) {

();

}

}

return data ;

}

/**

* :: Compression of byte[]

*

* @param The data to be compressed

* @return Compressed data

*/

public static byte[] compress(byte[] data) {

("before:" + );

GZIPOutputStream gzip = null ;

ByteArrayOutputStream baos = null ;

byte[] newData = null ;

try {

baos = new ByteArrayOutputStream() ;

gzip = new GZIPOutputStream(baos);

(data);

();

();

newData = () ;

} catch (IOException e) {

();

} finally {

try {

();

() ;

} catch (IOException e) {

();

}

}

("after:" + );

return newData ;

}

}

Finally output the length of the string, you may think that after compression does not reduce the volume of how much. But you can try without gzip, you will find that the converted string is much larger than the original. No way, this is determined by the Base64 algorithm. So, it is better to compress it.

The method used in this article is relatively simple, so if you have a better or feel there is a better way to do it, you may want to discuss it together.

Lastly, as a side note, I can't believe I wrote so many lines of code in Java. If I had used Python, I think I could have done it in a few lines.