JSON transfer binary array scheme
JSON is a very concise protocol, but unfortunately, it can only pass basic numbers (int,long,string, etc.), but cannot pass byte types. If you want to transfer pictures and other binary files, there is no way to transfer directly.
This article provides a way for everyone to reference, so that you can transfer binary files in JSON, if you have this demand and do not know how to achieve, perhaps this article can help you. Thought applies to all languages, this article in Java implementation, I believe that everyone can easily translate into their own understanding of language.
Ideas
1. Read binary files to memory
2. Compress it with gzip. After all, it is in the network transmission, of course, you can also do not compress.
3. Use Base64 to turn byte[] into a string
Add: What is Base64
Base64 is a coding method that converts 8-bit non-English characters to 7-bit ASCII characters. This is intended to satisfy the requirement that non-ASCII characters are not directly used in e-mail, but there are other important meanings as well:
A) All binaries can therefore be converted to printable text encoding and edited using text software;
b) The ability to encrypt text simply.
Realize
The main idea is that the above 3 steps, add the string to the JSON field after the server, and then use Base64 decryption –>gzip extract, you can get the original binary files. Isn't it simple? Say a lot, let's take a look at the specific code implementation.
Note: Java se is no way to directly use the Base64 Oh, you must first download a copy. But Android has integrated the Base64, so you can use it directly on Android.
/**
* @author Xing
*/
public class TestBase64 {
public static void Main (string[] args) {
byte[] data = Compress (LoadFile ());
String json = new String (base64.encodebase64 (data));
SYSTEM.OUT.PRINTLN ("Data length:" + json.length ());
}
/**
* Load local file and convert to byte array
* @return
*/
public static byte[] LoadFile () {
File File = new file ("D:/11.jpg");
FileInputStream FIS = null;
Bytearrayoutputstream BAOs = null;
byte[] data = null;
try {
FIS = new FileInputStream (file);
BAOs = new Bytearrayoutputstream ((int) file.length ());
byte[] buffer = new byte[1024];
int len =-1;
while (len = fis.read (buffer))! =-1) {
Baos.write (buffer, 0, Len);
}
data = Baos.tobytearray ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
if (FIS! = null) {
Fis.close ();
FIS = null;
}
Baos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
return data;
}
/**
* byte[] Compression
*
* @param data to compress
* @return Compressed data
*/
public static byte[] Compress (byte [] data) {
System.out.println ("Before:" + data.length);
Gzipoutputstream gzip = null;
Bytearrayoutputstream BAOs = null;
byte[] NewData = null;
try {
BAOs = new Bytearrayoutputstream ();
gzip = new Gzipoutputstream (BAOs);
Gzip.write (data);
Gzip.finish ();
Gzip.flush ();
NewData = Baos.tobytearray ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
Gzip.close ();
Baos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
System.out.println ("after:" + newdata.length);
return newdata;
}
}
Finally output the string length, you may think that after compression has not lowered how much volume. 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's better to compress it.
The method used in this article is relatively simple, if you have a better or feel a better way, you might as well discuss it together.
JSON binary transfer scheme