Java 工作 sun.misc.BASE64Encoder/Decoder 获取字节 []

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2267036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:27:32  来源:igfitidea点击:

work sun.misc.BASE64Encoder/Decoder for getting byte[]

java

提问by slavig

I am trying to use sun.misc.BASE64Encoder/Decoder, but this code:

我正在尝试使用 sun.misc.BASE64Encoder/Decoder,但这段代码:

(new sun.misc BASE64Encoder()).encode(new    
    sun.misc.BASE64Decoder().decodeBuffer("test string XML:"))

returns "test/string/XML/" I am embarrassed

返回 "test/string/XML/" 我很尴尬

采纳答案by Bozho

Don't use sun.miscor com.sunclasses. They are not guaranteed to be consistent between different versions of the jre.

不要使用sun.misccom.sun类。不保证它们在不同版本的 jre 之间是一致的。

Use commons-codecBase64.encodeBase64(..)and Base64.decodeBase64(..)

使用公共编解码器Base64.encodeBase64(..)Base64.decodeBase64(..)

回答by George Armhold

I think you want:

我想你想要:

String s = "Hello world";
new sun.misc.BASE64Encoder().encode(s.getBytes("UTF-8"));

Even better, use the commons utils as the previous answer suggested. Only use sun.misc.Base64Encoder if you can't afford to add the external dependency on another jar.

更好的是,按照上一个答案的建议使用 commons utils。如果您负担不起在另一个 jar 上添加外部依赖项,请仅使用 sun.misc.Base64Encoder。

回答by JSB????

You first decodingthe string "test string XML:", which isn't really valid Base64, since it contains spaces and a colon, none of which are valid B64 characters. I think that you meant to encodethen decode, like this:

您首先解码字符串“test string XML:”,它不是真正有效的 Base64,因为它包含空格和冒号,而这些都不是有效的 B64 字符。我认为您打算先编码然后再解码,如下所示:

(new sun.misc.BASE64Decoder().decodeBuffer(new sun.misc.BASE64Encoder().encode("test string XML:"))

回答by Diego

Use Class:

使用等级:

javax.xml.bind.DatatypeConverter

It has 2 methods of interest:

它有两种感兴趣的方法:

public static byte[] parseBase64Binary( String lexicalXSDBase64Binary )
public static String printBase64Binary( byte[] val )