Base64
static class Base64.Encoder
该类实现一个编码器,使用 Base64 编码来编码字节数据。
static class Base64.Decoder
该类实现一个解码器,使用 Base64 编码来解码字节数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public static void main(String args[]){ try{ String base64encodedString = Base64.getEncoder().encodeToString("runoob?java8".getBytes("utf-8")); System.out.println("Base64 编码字符串 (基本) :" + base64encodedString); byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
System.out.println("原始字符串: " + new String(base64decodedBytes, "utf-8")); base64encodedString = Base64.getUrlEncoder().encodeToString("runoob?java8".getBytes("utf-8")); System.out.println("Base64 编码字符串 (URL) :" + base64encodedString);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 10; ++i) { stringBuilder.append(UUID.randomUUID().toString()); }
byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8"); String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes); System.out.println("Base64 编码字符串 (MIME) :" + mimeEncodedString); }catch(UnsupportedEncodingException e){ System.out.println("Error :" + e.getMessage()); } }
|