栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将字节数组放入JSON,反之亦然

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将字节数组放入JSON,反之亦然

这是base64编码字节数组的一个很好的示例。当您在组合中添加unipre字符以发送诸如PDF文档之类的内容时,情况变得更加复杂。对字节数组进行编码后,可以将编码后的字符串用作JSON属性值。

Apache Commons提供了很好的实用程序:

 byte[] bytes = getByteArr(); String base64String = base64.enprebase64String(bytes); byte[] backToBytes = base64.deprebase64(base64String);

https://developer.mozilla.org/zh-
CN/docs/Web/Javascript/base64_encoding_and_decoding

Java服务器端示例:

public String getUnsecureContentbase64(String url)        throws ClientProtocolException, IOException { //getUnsecureContent will generate some byte[]    byte[] result = getUnsecureContent(url); // use apache org.apache.commons.prec.binary.base64 // if you're sending back as a http request result you may have to // org.apache.commons.httpclient.util.URIUtil.enpreQuery    return base64.enprebase64String(result);}

Javascript解码:

//depre URL encoding if enpred before returning resultvar uriEnpredString = depreURIComponent(response);var byteArr = base64DecToArr(uriEnpredString);//from mozillafunction b64ToUint6 (nChr) {  return nChr > 64 && nChr < 91 ?      nChr - 65    : nChr > 96 && nChr < 123 ?      nChr - 71    : nChr > 47 && nChr < 58 ?      nChr + 4    : nChr === 43 ?      62    : nChr === 47 ?      63    :      0;}function base64DecToArr (sbase64, nBlocksSize) {  var    sB64Enc = sbase64.replace(/[^A-Za-z0-9+/]/g, ""), nInLen = sB64Enc.length,    nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);  for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {    nMod4 = nInIdx & 3;    nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;    if (nMod4 === 3 || nInLen - nInIdx === 1) {      for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {        taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;      }      nUint24 = 0;    }  }  return taBytes;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410104.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号