业务描述:
模拟终端(智能家居)发送HTTP POST请求,请求参数为二进制流;而且,二进制流是加密后的数据,因此调试分两步:
1、Java代码生成加密后数据,并保存为二进制流文件 (电脑上的图片就是二进制流文件,只不过我们打开的时候,借助于电脑软件渲染为图片)
上代码片段:
1 byte[] oneshotBytes =reqParms.getBytes();2 log.info("length----->"+oneshotBytes.length);3 //FileWriter fw = new FileWriter(new File("/home/unisound/private_cloud_platform/rtc_demo/req_binary"));4 //fw.write(new String(oneshotBytes,"utf-8"));5 //fw.flush();6 //fw.close();
7 OutputStream os = new FileOutputStream(new File("/home/unisound/private_cloud_platform/rtc_demo/req_binary"));8 os.write(oneshotBytes);9 os.flush();10 os.close();
代码很简单,就是把byte[]保存为文件;
坑点:千万不要把byte[] 转换为 String 然后把 String写入文件;不然你会发现,原本214字节的文件变为252字节或者更大!!!
原因很简单,二进制流转换为字符串,这是编码的过程ÿ