本文轉自:http://wiyi.org/binary-to-string.htmlhtml
json 是一種很簡潔的協議,但惋惜的是,它只能傳遞基本的數型(int,long,string等),但不能傳遞byte類型。若是想要傳輸圖片等二進制文件的話,是沒辦法直接傳輸。java
本文提供一種思路給你們參考,讓你們能夠在json傳輸二進制文件,若是你們有這個需求又不知怎麼實現的話,也許本文可以幫到你。思想適用於全部語言,本文以java實現,相信你們很容易就能轉化爲本身懂得語言。算法
思路
1. 讀取二進制文件到內存json
2. 用Gzip壓縮一下。畢竟是在網絡傳輸嘛,固然你也能夠不壓縮。數組
3. 用Base64 把byte[] 轉成字符串服務器
補充:什麼是Base64
如下摘自阮一峯博客,Base64的具體編碼方式,你們能夠直接進入。網絡
Base64是一種編碼方式,它能夠將8位的非英語字符轉化爲7位的ASCII字符。這樣的初衷,是爲了知足電子郵件中不能直接使用非ASCII碼字符的規定,可是也有其餘重要的意義:編碼
a)全部的二進制文件,均可以所以轉化爲可打印的文本編碼,使用文本軟件進行編輯;加密
b)可以對文本進行簡單的加密。spa
實現
主要思路就是以上3步,把字符串添加到json字段後發給服務端,而後服務器再用Base64解密–>Gzip解壓,就能獲得原始的二進制文件了。是否是很簡單呢?說了很多,下面咱們來看看具體的代碼實現。
***注:Java SE是沒辦法直接用Base64的哦,必需要先本身去下載一份。但Android已經集成了Base64,所以你們能夠直接在Android使用。
- 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());
- }
-
-
- 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 ;
- }
-
-
- 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 ;
- }
- }
最後輸出了一下字符串長度,你們也許以爲通過壓縮也沒下降多少體積嘛。但你們能夠試試不用gzip,你會發現通過轉換的字符串比原來大多了。沒辦法,這是由Base64的算法決定的。因此嘛,仍是壓縮一下好。
本文所使用的方法比較簡單,你們若是有更好或者以爲有更好的方式,不妨一塊兒探討一下。
最後順便吐槽一下Java,居然寫了這麼多行代碼。要是用Python,估計沒幾行就能搞定了。