可以使用包将字符串解码为 。下面我用于编码和解码的代码。java.util.Base64
byte[]
对于 Java 8 :
import java.io.UnsupportedEncodingException;
import java.util.Base64;
public class Example {
public static void main(String[] args) {
try {
byte[] name = Base64.getEncoder().encode("hello World".getBytes());
byte[] decodedString = Base64.getDecoder().decode(new String(name).getBytes("UTF-8"));
System.out.println(new String(decodedString));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
对于 Java 6 :
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
public class Main {
public static void main(String[] args) {
try {
byte[] name = Base64.encodeBase64("hello World".getBytes());
byte[] decodedString = Base64.decodeBase64(new String(name).getBytes("UTF-8"));
System.out.println(new String(decodedString));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}