问题描述
Java 8 库有两个可用于URI构建的变体:基本和URL和文件名安全。该文档指向表2作为差异的解释。
The Java 8 Base64 library has two variants that can be used in URI building: the "Basic" one and the "URL and Filename safe". The documentation points to RFC 4648 Table 2 as an explanation for the differences.
在阅读规范之后,我仍然不清楚两种编码之间的实际差异:两种标准都得到广泛支持吗?具体的浏览器呢?是否建议对数据URI编码使用URL和文件名安全编码?是否有已知的支持限制?
After reading the spec it still isn't clear to me what the practical difference is between both encodings: are both standards "widely" supported? What about browsers specifically? Is the URL and filename safe encoding recommended for data URI encoding? Are there known support limitations?
推荐答案
最简单的方法是提供一个示例(恕我直言):
The easiest way is to provide an example(IMHO):
Base64.Encoder enc = Base64.getEncoder();
Base64.Encoder encURL = Base64.getUrlEncoder();
byte[] bytes = enc.encode("subjects?_d".getBytes());
byte[] bytesURL = encURL.encode("subjects?_d".getBytes());
System.out.println(new String(bytes)); // c3ViamVjdHM/X2Q= notice the "/"
System.out.println(new String(bytesURL)); // c3ViamVjdHM_X2Q= notice the "_"
Base64.Decoder dec = Base64.getDecoder();
Base64.Decoder decURL = Base64.getUrlDecoder();
byte[] decodedURL = decURL.decode(bytesURL);
byte[] decoded = dec.decode(bytes);
System.out.println(new String(decodedURL));
System.out.println(new String(decoded));
注意一个 URL安全
和其他不是。
事实上,如果你看一下实现,有两个用于编码的查找表: toBase64
和 toBase64URL
。只有两个字符不同:
As a matter of fact if you look at the implementation, there are two look-up tables used for encoding: toBase64
and toBase64URL
. There are two characters only that differ for them:
+
和 /
for toBase64
与 -
和 _
for toBase64URL
。
+
and /
for toBase64
versus -
and _
for toBase64URL
.
所以看来你的问题是一个安全的URI,应该在那里使用吗? / em>的;答案是肯定的。
So it seems that your question is one URI safe and should be used there?; the answer is yes.
这篇关于Java 8中basic和url base64编码之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!