Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 工作sun.misc.base64编码器/解码器以获取字节[]_Java - Fatal编程技术网

Java 工作sun.misc.base64编码器/解码器以获取字节[]

Java 工作sun.misc.base64编码器/解码器以获取字节[],java,Java,我正在尝试使用sun.misc.base64编码器/解码器,但以下代码: (new sun.misc BASE64Encoder()).encode(new sun.misc.BASE64Decoder().decodeBuffer("test string XML:")) 返回“test/string/XML/” 我很尴尬不要使用sun.misc或com.sun类。它们不能保证在不同版本的jre之间保持一致 使用Base64.encodeBase64(…)和Base64.de

我正在尝试使用sun.misc.base64编码器/解码器,但以下代码:

(new sun.misc BASE64Encoder()).encode(new    
    sun.misc.BASE64Decoder().decodeBuffer("test string XML:"))
返回“test/string/XML/”
我很尴尬

不要使用
sun.misc
com.sun
类。它们不能保证在不同版本的jre之间保持一致

使用
Base64.encodeBase64(…)
Base64.decodeBase64(…)
我想您需要:

String s = "Hello world";
new sun.misc.BASE64Encoder().encode(s.getBytes("UTF-8"));
更好的是,按照前面的答案使用commons utils。如果您负担不起在另一个jar上添加外部依赖项,请仅使用sun.misc.Base64Encoder。

首先解码字符串“test string XML:”,该字符串实际上不是有效的Base64,因为它包含空格和冒号,而这些都不是有效的B64字符。我想你的意思是先编码然后解码,就像这样:

(new sun.misc.BASE64Decoder().decodeBuffer(new sun.misc.BASE64Encoder().encode("test string XML:"))
使用类别:

javax.xml.bind.DatatypeConverter
它有两种有趣的方法:

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

呵呵。。我将我最近的项目sun.misc.base64编码器/解码器发送给一位客户。不知道不同jre中的不一致性。我希望这是一个罕见的问题。实际上,自JDK1.1以来,它一直运行良好,但使用内部API仍然很难看。从纯实用的角度来看,您无需担心。为什么不使用sun。*软件包:几天来,我一直在寻找使用本机库转换Base64的可靠方法。谢谢