This question with regard to JDK 5说,JDK5没有提供实现,但是JDK6应该有一个sun.misc.Base64Decoder。
不过,据我所知,JDK没有提供这个类,我在其中找不到任何其他类似的类
那么,JDK6的情况是怎样的呢?
我知道有许多像Commons和JBoss这样的实现,但我们有一个限制性的第三方库策略,所以我试图避免重复发明轮子。
发布于 2016-07-07 21:26:06
就像Joachim Sauer在之前的评论中所说的那样,JDK1.6已经与它自己的Base64实现(sun.misc.*)捆绑在一起了,下面是一个例子:
String toEncode = "Encoding and Decoding in Base64 Test";
//Encoding in b64
String encoded = new BASE64Encoder().encode(toEncode.getBytes());
System.out.println(encoded);
//Decoding in b64
byte[] decodeResult = new BASE64Decoder().decodeBuffer(encoded);
System.out.println(new String(decodeResult));发布于 2019-06-25 07:07:12
我使用了byte[] decodedValue = DatatypeConverter.parseBase64Binary(value);
这个网站解释得很好:https://www.rgagnon.com/javadetails/java-0598.html
摘录如下:
public static String encode(String value) throws Exception {
return DatatypeConverter.printBase64Binary
(value.getBytes(StandardCharsets.UTF_8)); // use "utf-8" if java 6
}
public static String decode(String value) throws Exception {
byte[] decodedValue = DatatypeConverter.parseBase64Binary(value);
return new String(decodedValue, StandardCharsets.UTF_8); // use "utf-8" if java 6
}https://stackoverflow.com/questions/5908574
复制相似问题