json傳輸二進制的方案【轉】

本文轉自: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使用。

 

[java] view plain copy
  1. /** 
  2.  * @author xing 
  3.  */  
  4. public class TestBase64 {  
  5.     public static void main(String[] args) {  
  6.         byte[] data = compress(loadFile());  
  7.   
  8.         String json = new String(Base64.encodeBase64(data));  
  9.         System.out.println("data length:" + json.length());  
  10.     }  
  11.       
  12.     /** 
  13.      * 加載本地文件,並轉換爲byte數組 
  14.      * @return 
  15.      */  
  16.     public static byte[] loadFile() {  
  17.         File file = new File("d:/11.jpg");  
  18.   
  19.         FileInputStream fis = null;  
  20.         ByteArrayOutputStream baos = null;  
  21.         byte[] data = null ;  
  22.   
  23.         try {  
  24.             fis = new FileInputStream(file);  
  25.             baos = new ByteArrayOutputStream((int) file.length());  
  26.   
  27.             byte[] buffer = new byte[1024];  
  28.             int len = -1;  
  29.             while ((len = fis.read(buffer)) != -1) {  
  30.                 baos.write(buffer, 0, len);  
  31.             }  
  32.               
  33.             data = baos.toByteArray() ;  
  34.   
  35.         } catch (IOException e) {  
  36.             e.printStackTrace();  
  37.         } finally {  
  38.             try {  
  39.                 if (fis != null) {  
  40.                     fis.close();  
  41.                     fis = null;  
  42.                 }  
  43.                   
  44.                 baos.close() ;  
  45.             } catch (IOException e) {  
  46.                 e.printStackTrace();  
  47.             }  
  48.         }  
  49.           
  50.         return data ;  
  51.     }  
  52.       
  53.     /** 
  54.      * 對byte[]進行壓縮 
  55.      *  
  56.      * @param 要壓縮的數據 
  57.      * @return 壓縮後的數據 
  58.      */  
  59.     public static byte[] compress(byte[] data) {  
  60.         System.out.println("before:" + data.length);  
  61.           
  62.         GZIPOutputStream gzip = null ;  
  63.         ByteArrayOutputStream baos = null ;  
  64.         byte[] newData = null ;  
  65.           
  66.         try {  
  67.             baos = new ByteArrayOutputStream() ;  
  68.             gzip = new GZIPOutputStream(baos);  
  69.               
  70.             gzip.write(data);  
  71.             gzip.finish();  
  72.             gzip.flush();  
  73.               
  74.             newData = baos.toByteArray() ;  
  75.         } catch (IOException e) {  
  76.             e.printStackTrace();  
  77.         } finally {  
  78.             try {  
  79.                 gzip.close();  
  80.                 baos.close() ;  
  81.             } catch (IOException e) {  
  82.                 e.printStackTrace();  
  83.             }  
  84.         }  
  85.           
  86.         System.out.println("after:" + newData.length);  
  87.         return newData ;  
  88.     }  
  89. }  


最後輸出了一下字符串長度,你們也許以爲通過壓縮也沒下降多少體積嘛。但你們能夠試試不用gzip,你會發現通過轉換的字符串比原來大多了。沒辦法,這是由Base64的算法決定的。因此嘛,仍是壓縮一下好。

本文所使用的方法比較簡單,你們若是有更好或者以爲有更好的方式,不妨一塊兒探討一下。

最後順便吐槽一下Java,居然寫了這麼多行代碼。要是用Python,估計沒幾行就能搞定了。

相關文章
相關標籤/搜索