当前位置: 首页 > 面试题库 >

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

呼延明朗
2023-03-14
问题内容

我喜欢将字符串的Java映射编码为单个base
64编码的字符串。编码后的字符串将被传输到远程端点,并且可能由一个不好的人操纵。因此,应该发生的最糟糕的事情是密钥,值元组无效,但不应将任何其他安全风险置于一旁。

例:

Map<String,String> map = ...
String encoded = Base64.encode(map);

// somewhere else
Map<String,String> map = Base64.decode(encoded);

是的,必须是Base64。不喜欢那个
或那个
或其他任何一个。是否有现有的轻量级解决方案(首选单一实用程序类)?还是我必须创建自己的?

有什么比这更好的了吗?

// 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<String,String>) ois.readObject();
ois.close();

谢谢,


问题答案:

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

使用Google Gson转换Map为JSON。使用GZIPOutputStream压缩JSON字符串。使用Apache
Commons
CodecBase64
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);
}


 类似资料:
  • 问题内容: 我有一个PHP脚本,可以将PNG图像编码为Base64字符串。 我想使用JavaScript做同样的事情。我知道如何打开文件,但不确定如何进行编码。我不习惯使用二进制数据。 问题答案: 您可以使用和来与base64编码进行相互转换。 关于这些功能接受/返回的内容,评论中似乎有些混乱,所以…… 接受一个“字符串”,其中每个字符代表一个8位字节-如果传递的字符串包含不能以8位表示的字符,则

  • 问题内容: 我想将字符串转换为Base64。我在几个地方都找到了答案,但是在Swift中它不再起作用了。我正在使用Xcode 6.2。我相信答案可能适用于以前的Xcode版本,而不适用于Xcode 6.2。 有人可以指导我在Xcode 6.2中执行此操作吗? 我发现的答案是这样,但是在我的Xcode版本中不起作用: 参考:http : //iosdevelopertips.com/swift-co

  • 我想把base64编码的字符串转换成位图,这样我就可以把它放在图像视图中,但是得到的错误像

  • 问题内容: 我需要在GWT中将短字符串编码为base 64,并在服务器上解码base 64字符串。有人为此有实用程序类或库吗? 问题答案: 您可以在除IE≤9之外的所有浏览器上的客户端上使用本机JavaScript,在服务器上可以使用官方类之一。 Java / GWT: 编码为。

  • 我正在尝试将yaml文件作为base64字符串发送,以便此代码正常工作: 在上述代码应包含数据。 我有这条路线: 代码工作正常,但客户机代码希望在以下代码中名为的属性中使用base64字符串: 但是字符串处于。如何设置属性?

  • 问题内容: 我已经通过AJAX向PHP发送了base64编码的字符串,并创建了一个图像资源-一切都很好。 现在,我想在调整图像大小后获得base64编码的字符串,但是我无法找到一个函数来获取base64encoded字符串。 问题答案: 取自http://www.php.net/manual/zh/book.image.php#93393