如何将Map 编码为Base64字符串?

我喜欢将字符串的java映射编码为单个基本64位编码字符串。 编码的字符串将被传输到远程端点,并且可能由不好的人操纵。 所以应该发生的最糟糕的事情是invaild key,value-tuples,但不应该带来任何其他安全风险。

例:

Map map = ... String encoded = Base64.encode(map); // somewhere else Map map = Base64.decode(encoded); 

是的,必须是Base64。 不喜欢那个 或那个 或任何其他这些 。 是否有现有的轻量级解决方案(Single Utils-Class首选)? 或者我必须创建自己的?

有什么比这更好的?

 // marshalling ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(map); oos.close(); String encoded = new String(Base64.encodeBase64(baos.toByteArray())); // unmarshalling byte[] decoded = Base64.decodeBase64(encoded.getBytes()); ByteArrayInputStream bais = new ByteArrayInputStream(decoded); ObjectInputStream ois = new ObjectInputStream(bais); map = (Map) ois.readObject(); ois.close(); 

谢谢,

我的主要要求是:编码字符串应尽可能短,并且只包含来自base64字母表的拉丁字符或字符(不是我的调用)。 没有其他要求。

使用Google Gson将Map转换为JSON 。 使用GZIPOutputStream压缩JSON字符串。 使用Apache Commons Codec Base64Base64OutputStream将压缩字节编码为Base64字符串。

开球示例:

 public static void main(String[] args) throws IOException { Map map = new HashMap(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); String serialized = serialize(map); Map deserialized = deserialize(serialized, new TypeToken>() {}.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 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); } 

另一种可能的方法是使用JSON ,这是一个非常轻量级的lib。 然后编码看起来像这样:

 JSONObject jso = new JSONObject( map ); String encoded = new String(Base64.encodeBase64( jso.toString( 4 ).toByteArray())); 

你的解决方案有效 唯一的另一种方法是自己序列化地图(迭代键和值)。 这意味着你必须确保正确处理所有情况(例如,如果你将值传递为key=value ,你必须找到一种方法来允许在键/值中使用= ,你必须以某种方式分离这些对这意味着您还必须在名称等中允许此分隔字符。

总而言之,很难做到正确,容易出错,而且需要更多的代码和头痛。 另外,请不要忘记您必须在解析器(接收方)中编写大量error handling代码。