本文转自:http://wiyi.org/binary-to-string.html

json 是一种很简洁的协议,但可惜的是,它只能传递基本的数型(int,long,string等),但不能传递byte类型。如果想要传输图片等二进制文件的话,是没办法直接传输。

本文提供一种思路给大家参考,让大家可以在json传输二进制文件,如果大家有这个需求又不知怎么实现的话,也许本文能够帮到你。思想适用于所有语言,本文以java实现,相信大家很容易就能转化为自己懂得语言。

思路

1. 读取二进制文件到内存

2. 用Gzip压缩一下。毕竟是在网络传输嘛,当然你也可以不压缩。

3. 用Base64 把byte[] 转成字符串

补充:什么是Base64

以下摘自阮一峰博客,Base64的具体编码方式,大家可以直接进入

Base64是一种编码方式,它可以将8位的非英语字符转化为7位的ASCII字符。这样的初衷,是为了满足电子邮件中不能直接使用非ASCII码字符的规定,但是也有其他重要的意义:

a)所有的二进制文件,都可以因此转化为可打印的文本编码,使用文本软件进行编辑;

b)能够对文本进行简单的加密。

实现

主要思路就是以上3步,把字符串添加到json字段后发给服务端,然后服务器再用Base64解密–>Gzip解压,就能得到原始的二进制文件了。是不是很简单呢?说了不少,下面我们来看看具体的代码实现。

***注:Java SE是没办法直接用Base64的哦,必须要先自己去下载一份。但Android已经集成了Base64,因此大家可以直接在Android使用。

  1. /**
  2. * @author xing
  3. */
  4. public class TestBase64 {
  5. public static void main(String[] args) {
  6. byte[] data = compress(loadFile());
  7. String json = new String(Base64.encodeBase64(data));
  8. System.out.println("data length:" + json.length());
  9. }
  10. /**
  11. * 加载本地文件,并转换为byte数组
  12. * @return
  13. */
  14. public static byte[] loadFile() {
  15. File file = new File("d:/11.jpg");
  16. FileInputStream fis = null;
  17. ByteArrayOutputStream baos = null;
  18. byte[] data = null ;
  19. try {
  20. fis = new FileInputStream(file);
  21. baos = new ByteArrayOutputStream((int) file.length());
  22. byte[] buffer = new byte[1024];
  23. int len = -1;
  24. while ((len = fis.read(buffer)) != -1) {
  25. baos.write(buffer, 0, len);
  26. }
  27. data = baos.toByteArray() ;
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. } finally {
  31. try {
  32. if (fis != null) {
  33. fis.close();
  34. fis = null;
  35. }
  36. baos.close() ;
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. return data ;
  42. }
  43. /**
  44. * 对byte[]进行压缩
  45. *
  46. * @param 要压缩的数据
  47. * @return 压缩后的数据
  48. */
  49. public static byte[] compress(byte[] data) {
  50. System.out.println("before:" + data.length);
  51. GZIPOutputStream gzip = null ;
  52. ByteArrayOutputStream baos = null ;
  53. byte[] newData = null ;
  54. try {
  55. baos = new ByteArrayOutputStream() ;
  56. gzip = new GZIPOutputStream(baos);
  57. gzip.write(data);
  58. gzip.finish();
  59. gzip.flush();
  60. newData = baos.toByteArray() ;
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. } finally {
  64. try {
  65. gzip.close();
  66. baos.close() ;
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. System.out.println("after:" + newData.length);
  72. return newData ;
  73. }
  74. }

最后输出了一下字符串长度,大家也许觉得经过压缩也没降低多少体积嘛。但大家可以试试不用gzip,你会发现经过转换的字符串比原来大多了。没办法,这是由Base64的算法决定的。所以嘛,还是压缩一下好。

本文所使用的方法比较简单,大家如果有更好或者觉得有更好的方式,不妨一起探讨一下。

最后顺便吐槽一下Java,竟然写了这么多行代码。要是用Python,估计没几行就能搞定了。

最新文章

  1. 为什么volatile不能保证原子性而Atomic可以?
  2. Windows Phone Foreground Toast
  3. (转)PowerDesigner提示Existence of index、key、reference错误
  4. nyoj 712 探 寻 宝 藏--最小费用最大流
  5. placehold.it-在线图片生成器(转载)
  6. MyBatis学习笔记之resultMap
  7. 我理解的RPC
  8. L12 samba服务器搭建
  9. SQL点滴14—编辑数据
  10. angular directive
  11. DSAPI实现简单的透明窗体
  12. studio配置本地gradle-x.x.x-all.zip
  13. linux 添加本地yum源
  14. Linux学习笔记-基本操作1
  15. bzoj千题计划174:bzoj1800: [Ahoi2009]fly 飞行棋
  16. 《jquery实战》javascript 必知必会(2)
  17. linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体[转]
  18. Jvm中时区设置方式
  19. MongoDB 数据管理
  20. 使用 CLI 创建 Azure VM 的自定义映像

热门文章

  1. [转]C++ STL list的初始化、添加、遍历、插入、删除、查找、排序、释放
  2. 【Ray Tracing in One Weekend 超详解】 光线追踪1-10
  3. springmvc学习总结(一) -- 从零搭建,基础入门
  4. 模板 树上求LCA 倍增和树链剖分
  5. java并发基础(六)--- 活跃性、性能与可伸缩性
  6. 通过本地Git部署网站到WebSite
  7. Android应用程序模型:应用程序,任务,进程,线程
  8. TimingTool - The Timing Diagram Editor
  9. 《Go语言实战》摘录:6.5 并发 - 通道
  10. 没有可用的复制构造函数或复制构造函数声明为“explicit”