sun.misc.BASE64Encoder/Decoder用于获取byte []

sla*_*vig 9 java

我试图使用sun.misc.BASE64Encoder/Decoder,但是这段代码:

(new sun.misc BASE64Encoder()).encode(new    
    sun.misc.BASE64Decoder().decodeBuffer("test string XML:"))
Run Code Online (Sandbox Code Playgroud)

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

Boz*_*zho 22

不要使用sun.misccom.sun上课.它们不能保证在不同版本的jre之间保持一致.

使用commons-codec Base64.encodeBase64(..)Base64.decodeBase64(..)

  • 从JDK 1.1开始,它实际上工作正常,但使用内部API仍然很难看.从纯粹实用的角度来看,您无需担心. (2认同)
  • 为什么不使用sun.*包:http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html (2认同)

小智 15

使用类:

javax.xml.bind.DatatypeConverter
Run Code Online (Sandbox Code Playgroud)

它有两种感兴趣的方法:

public static byte[] parseBase64Binary( String lexicalXSDBase64Binary )
public static String printBase64Binary( byte[] val )
Run Code Online (Sandbox Code Playgroud)


JSB*_*ոգչ 7

您首先解码字符串"test string XML:",它不是真正有效的Base64,因为它包含空格和冒号,其中没有一个是有效的B64字符.我认为你打算编码然后解码,像这样:

(new sun.misc.BASE64Decoder().decodeBuffer(new sun.misc.BASE64Encoder().encode("test string XML:"))
Run Code Online (Sandbox Code Playgroud)

  • encode只接受byte [],而不是String. (2认同)