Table of Contents
ToggleJava Base64 Encoding and Decoding
在 Java 8 現在具有用於 Base64 編碼的內建編碼器和解碼器,其中之一基本編碼器,會將輸出映射到 A-Za-z0-9+/ 中的一組字元,編碼器不會在輸出中添加任何換行,解碼器會拒絕 A-Za-z0-9+/ 之外的任何字元,同時也使用 Apache Commons Codec 的實用 API 進行編解碼, Base64 Encoding and Decoding in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- base64
| +- basic
| +- EncodingDecodingTest.java
單元測試
Java Base64 Encoder Decoder 提供 Base64 基本編碼器操作,將位元組編碼成 Base64 ,並將 Base64 字串解碼為普通字串。
encode
建立一個 String ,轉成 byte[] ,使用 Base64.Encoder 進行編碼。
@Test
public void encode() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
System.out.println(value);
byte[] result = encoder.encode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
[B@6aa8ceb6
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
decode
Java Base64 Encoder Decoder 建立一個 String ,轉成 byte[] ,使用 Base64.Decoder 進行解碼。
@Test
public void decode() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
byte[] result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
[B@6aa8ceb6
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
encodeWithoutPadding
Java Base64 Encoder Decoder 建立一個 String ,轉成 byte[] ,使用 Base64.Encoder 不進行填充的編碼。
@Test
public void encodeWithoutPadding() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
System.out.println(value);
byte[] result = encoder.withoutPadding().encode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA");
}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
[B@6aa8ceb6
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA
decodeWithoutPadding
Java Base64 Encoder Decoder 建立一個 String ,編碼不進行填充 ,轉成 byte[] ,使用 Base64.Decoder 解碼。
@Test
public void decodeWithoutPadding() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA";
System.out.println(value);
byte[] result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA
[B@6aa8ceb6
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
Base64_encodeBase64
建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行編碼。
@Test
public void Base64_encodeBase64() throws Exception {
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64
.encodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
[B@4c98385c
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
Base64_decodeBase64
建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行解碼。
@Test
public void Base64_decodeBase64() throws Exception {
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64
.decodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
[B@4c98385c
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
EncodingDecodingTest.java
Base64 Encoder Decoder in Java 新增單元測試,驗證 Java Base64 Encoding Decoding 是否符合預期。
package org.ruoxue.java_147.base64.basic;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.junit.Test;
public class EncodingDecodingTest {
@Test
public void encode() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
System.out.println(value);
byte[] result = encoder.encode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
@Test
public void decode() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
byte[] result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
@Test
public void encodeWithoutPadding() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
System.out.println(value);
byte[] result = encoder.withoutPadding().encode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA");
}
@Test
public void decodeWithoutPadding() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA";
System.out.println(value);
byte[] result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
@Test
public void Base64_encodeBase64() throws Exception {
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64
.encodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
@Test
public void Base64_decodeBase64() throws Exception {
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64
.decodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
}
心得分享
Java Base64 Encoding Decoding 依照 RFC 4648 中指定的 Base64 字母表,輸出映射到一組字元 A-Za-z0-9+/ ,不添加任何行分隔符號,熟悉 Base64 Encoder Decoder in Java 工具類別提供的靜態方法,取得基本編碼器,進行編碼解碼的操作。