Table of Contents
ToggleBase64 Encode and Decode in Java with Examples
從 Java 8 以來,已經提供 Base64 編碼器和解碼器,基本、 MIME 和 URL 三種編碼器,將二進位資料可視化,用 64 種字元組合成字串,而這個 Binary 資料和字串資料彼此之間是可以互相轉換的,十分方便,在實際應用上,常用來表示資料加密過後的內容及網路傳輸的內容, Base64 Java Encode Decode 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- base64
| +- EncodeDecodeExamplesTest.java
單元測試
Java Encode Decode Base64 提供 Base64 基本編碼器操作,將位元組編碼成 Base64 ,並將 Base64 字串解碼為普通字串。
encodeDecode
建立一個 String ,轉成 byte[] ,使用 Base64.Encoder 進行編碼,然後再使用 Base64.Decoder 進行解碼。
@Test
public void encodeDecode() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
System.out.println(value);
String encodedString = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedString);
assertThat(encodedString)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODktXw==");
Base64.Decoder decoder = Base64.getDecoder();
byte[] result = decoder.decode(encodedString);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODktXw==
[B@1b0375b3
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
Base64_encodeDecode
Java Encode Decode Base64 建立一個 String ,使用 Apache Commons Codec 套件 Base64 進行編碼,然後再進行解碼。
@Test
public void Base64_encodeDecode() throws Exception {
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
System.out.println(value);
byte[] encodedByteArray = org.apache.commons.codec.binary.Base64
.encodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedByteArray);
System.out.println(new String(encodedByteArray));
assertThat(new String(encodedByteArray))
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODktXw==");
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(encodedByteArray);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
[B@1a93a7ca
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODktXw==
[B@b684286
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
mimeEncodeDecode
Java Encode Decode Base64 建立一個 String ,轉成 byte[] ,使用 Base64.MimeEncoder 進行編碼,然後再使用 Base64.MimeDecoder 進行解碼。
@Test
public void mimeEncodeDecode() throws Exception {
Base64.Encoder encoder = Base64.getMimeEncoder();
StringBuilder value = new StringBuilder();
value.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
System.out.println(value);
String encodedString = encoder.encodeToString(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedString);
assertThat(encodedString).containsIgnoringNewLines(
"QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0\n" + "NTY3ODktXw==");
Base64.Decoder decoder = Base64.getMimeDecoder();
byte[] result = decoder.decode(encodedString);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0
NTY3ODktXw==
[B@7c75222b
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
Base64_mimeEncodeDecode
Java Encode Decode Base64 建立一個 String ,使用 Apache Commons Codec 套件 Base64 進行編碼,然後再進行解碼。
@Test
public void Base64_mimeEncodeDecode() throws Exception {
StringBuilder value = new StringBuilder();
value.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
System.out.println(value);
byte[] encodedByteArray = org.apache.commons.codec.binary.Base64
.encodeBase64Chunked(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedByteArray);
System.out.println(new String(encodedByteArray));
assertThat(new String(encodedByteArray)).containsIgnoringNewLines(
"QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0\n" + "NTY3ODktXw==");
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(encodedByteArray);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
[B@1a93a7ca
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0
NTY3ODktXw==
[B@30c7da1e
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
urlEncodeDecode
Base64 Java Encoder Decoder 建立一個 String ,轉成 byte[] ,使用 Base64.UrlEncoder 進行編碼,然後再使用 Base64.UrlDecoder 進行解碼。
@Test
public void urlEncodeDecode() throws Exception {
Base64.Encoder encoder = Base64.getUrlEncoder();
String value = "https://www.ruoxue.org/category/rd/java/java-base64/";
System.out.println(value);
String encodedString = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedString);
assertThat(encodedString).isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9jYXRlZ29yeS9yZC9qYXZhL2phdmEtYmFzZTY0Lw==");
Base64.Decoder decoder = Base64.getUrlDecoder();
byte[] result = decoder.decode(encodedString);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("https://www.ruoxue.org/category/rd/java/java-base64/");
}
https://www.ruoxue.org/category/rd/java/java-base64/
aHR0cHM6Ly93d3cucnVveHVlLm9yZy9jYXRlZ29yeS9yZC9qYXZhL2phdmEtYmFzZTY0Lw==
[B@1b0375b3
https://www.ruoxue.org/category/rd/java/java-base64/
Base64_urlEncodeDecode
Base64 Java Encoder Decoder 建立一個 String ,使用 Apache Commons Codec 套件 Base64 進行編碼,然後再進行解碼。
@Test
public void Base64_urlEncodeDecode() throws Exception {
String value = "https://www.ruoxue.org/category/rd/java/java-base64/";
System.out.println(value);
byte[] encodedByteArray = org.apache.commons.codec.binary.Base64
.encodeBase64URLSafe(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedByteArray);
System.out.println(new String(encodedByteArray));
assertThat(new String(encodedByteArray))
.isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9jYXRlZ29yeS9yZC9qYXZhL2phdmEtYmFzZTY0Lw");
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(encodedByteArray);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("https://www.ruoxue.org/category/rd/java/java-base64/");
}
https://www.ruoxue.org/category/rd/java/java-base64/
[B@1a93a7ca
aHR0cHM6Ly93d3cucnVveHVlLm9yZy9jYXRlZ29yeS9yZC9qYXZhL2phdmEtYmFzZTY0Lw
[B@b684286
https://www.ruoxue.org/category/rd/java/java-base64/
EncodeDecodeExamplesTest.java
Base64 Java Encoder Decoder 新增單元測試,驗證 Base64 Java Encode and Decode Example 是否符合預期。
package org.ruoxue.java_147.base64;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.junit.Test;
public class EncodeDecodeExamplesTest {
@Test
public void encodeDecode() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
System.out.println(value);
String encodedString = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedString);
assertThat(encodedString)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODktXw==");
Base64.Decoder decoder = Base64.getDecoder();
byte[] result = decoder.decode(encodedString);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
}
@Test
public void Base64_encodeDecode() throws Exception {
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
System.out.println(value);
byte[] encodedByteArray = org.apache.commons.codec.binary.Base64
.encodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedByteArray);
System.out.println(new String(encodedByteArray));
assertThat(new String(encodedByteArray))
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODktXw==");
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(encodedByteArray);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
}
@Test
public void mimeEncodeDecode() throws Exception {
Base64.Encoder encoder = Base64.getMimeEncoder();
StringBuilder value = new StringBuilder();
value.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
System.out.println(value);
String encodedString = encoder.encodeToString(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedString);
assertThat(encodedString).containsIgnoringNewLines(
"QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0\n" + "NTY3ODktXw==");
Base64.Decoder decoder = Base64.getMimeDecoder();
byte[] result = decoder.decode(encodedString);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
}
@Test
public void Base64_mimeEncodeDecode() throws Exception {
StringBuilder value = new StringBuilder();
value.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
System.out.println(value);
byte[] encodedByteArray = org.apache.commons.codec.binary.Base64
.encodeBase64Chunked(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedByteArray);
System.out.println(new String(encodedByteArray));
assertThat(new String(encodedByteArray)).containsIgnoringNewLines(
"QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0\n" + "NTY3ODktXw==");
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(encodedByteArray);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
}
@Test
public void urlEncodeDecode() throws Exception {
Base64.Encoder encoder = Base64.getUrlEncoder();
String value = "https://www.ruoxue.org/category/rd/java/java-base64/";
System.out.println(value);
String encodedString = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedString);
assertThat(encodedString).isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9jYXRlZ29yeS9yZC9qYXZhL2phdmEtYmFzZTY0Lw==");
Base64.Decoder decoder = Base64.getUrlDecoder();
byte[] result = decoder.decode(encodedString);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("https://www.ruoxue.org/category/rd/java/java-base64/");
}
@Test
public void Base64_urlEncodeDecode() throws Exception {
String value = "https://www.ruoxue.org/category/rd/java/java-base64/";
System.out.println(value);
byte[] encodedByteArray = org.apache.commons.codec.binary.Base64
.encodeBase64URLSafe(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(encodedByteArray);
System.out.println(new String(encodedByteArray));
assertThat(new String(encodedByteArray))
.isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9jYXRlZ29yeS9yZC9qYXZhL2phdmEtYmFzZTY0Lw");
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(encodedByteArray);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("https://www.ruoxue.org/category/rd/java/java-base64/");
}
}
心得分享
Base64 Java Encode and Decode Example 將字串編碼為 Base64,首先將字串轉換為 byte 陣列,然後使用 Encoder 將 byte 陣列傳給 encode 方法,反之要解碼 Base64 編碼的字串,則使用 Decoder ,將字串傳給 decode 方法,取得 byte 陣列,提供 Base64 Java Encoder Decoder 工具類別的靜態方法,使用基本編碼器,進行編碼解碼的操作。