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

如何编码地图 作为Base64字符串?

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

如何编码地图 作为Base64字符串?

我的主要要求是:编码的字符串应尽可能短,并且仅包含拉丁字符或base64字母中的字符(不包括我的电话)。 没有其他要求。

使用Google Gson转换

Map
为JSON。使用
GZIPOutputStream
压缩JSON字符串。使用Apache
Commons
Codec
base64

base64OutputStream
将压缩字节编码为base64字符串。

开球示例:

public static void main(String[] args) throws IOException {    Map<String, String> map = new HashMap<String, String>();    map.put("key1", "value1");    map.put("key2", "value2");    map.put("key3", "value3");    String serialized = serialize(map);    Map<String, String> deserialized = deserialize(serialized, new TypeToken<Map<String, String>>() {}.getType());    System.out.println(deserialized);}public static String serialize(Object object) throws IOException {    ByteArrayOutputStream byteaOut = new ByteArrayOutputStream();    GZIPOutputStream gzipOut = null;    try {        gzipOut = new GZIPOutputStream(new base64OutputStream(byteaOut));        gzipOut.write(new Gson().toJson(object).getBytes("UTF-8"));    } finally {        if (gzipOut != null) try { gzipOut.close(); } catch (IOException logOrIgnore) {}    }    return new String(byteaOut.toByteArray());}public static <T> T deserialize(String string, Type type) throws IOException {    ByteArrayOutputStream byteaOut = new ByteArrayOutputStream();    GZIPInputStream gzipIn = null;    try {        gzipIn = new GZIPInputStream(new base64InputStream(new ByteArrayInputStream(string.getBytes("UTF-8"))));        for (int data; (data = gzipIn.read()) > -1;) { byteaOut.write(data);        }    } finally {        if (gzipIn != null) try { gzipIn.close(); } catch (IOException logOrIgnore) {}    }    return new Gson().fromJson(new String(byteaOut.toByteArray()), type);}


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

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

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