对Base64编码的支持已经被加入到Java 8官方库中,这样不需要使用第三方库就可以进行Base64编码,例子代码如下:
package com.ehking.example.java8.newfeature.demo11;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* Created by shiyanwu on 2017/4/17.
* java8 测试base64
*/
public class Java8Base64 {
public static void main(String[] args) {
final String text = "Base64 finally in Java 8!";
final String encoded = Base64
.getEncoder()
.encodeToString(text.getBytes(StandardCharsets.UTF_8));
System.out.println(encoded);
final String decoded = new String(
Base64.getDecoder().decode(encoded),
StandardCharsets.UTF_8);
System.out.println(decoded);
}
}
网友评论