You need to change the import of your class:
(您需要更改课程的导入:)
import org.apache.commons.codec.binary.Base64;
And then change your class to use the Base64 class.
(然后更改您的类以使用Base64类。)
Here's some example code:
(这是一些示例代码:)
byte[] encodedBytes = Base64.encodeBase64("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));
Then read why you shouldn't use sun.* packages .
(然后阅读为什么不应该使用sun。*软件包 。)
Update (2016-12-16) (更新(2016-12-16))
You can now use java.util.Base64
with Java 8. First, import it as you normally do:
(现在,您可以将java.util.Base64
与Java 8一起使用。首先,像平常一样导入它:)
import java.util.Base64;
Then use the Base64 static methods as follows:
(然后使用Base64静态方法,如下所示:)
byte[] encodedBytes = Base64.getEncoder().encode("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));
If you directly want to encode string and get the result as encoded string, you can use this:
(如果您直接想要对字符串进行编码并将结果作为编码后的字符串获取,则可以使用以下方法:)
String encodeBytes = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());
See Java documentation for Base64 for more.
(有关更多信息,请参见Base64的Java文档 。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…