Table of Contents
ToggleBase64 Encoding and Decoding in Java with Examples
使用基本編碼器轉換成 Base64 ,是一種二進位到文字的編碼方案,會取得原始輸入的位元組,並使用字元 A-Z 、 a-z 、 0-9 以及 + 、 / 等,將其編碼,會根據需要在輸出末尾添加一兩個填充字元 (=),當需要在僅支援文字資料的系統上儲存,或傳輸二進位資料而不遺失或損壞資料時,可以使用 Base64 編碼,Java 8 套件 Base64 類別中提供了一個基本編碼器,可用於將字串編碼為 Base64,並將 Base64 字串解碼為普通字串, Base64 Java Encoding Decoding 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- base64
| +- basic
| +- EncodingDecodingExamplesTest.java
單元測試
Java Encoding Decoding Base64 提供 Base64 基本編碼器操作,將位元組編碼成 Base64 ,並將 Base64 字串解碼為普通字串。
encodeToString
建立一個 String ,轉成 byte[] ,使用 Base64.Encoder 進行編碼。
@Test
public void encodeToString() 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);
String result = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
decodeWithString
Java Encoding Decoding Base64 建立一個 String ,使用 Base64.Decoder 進行解碼。
@Test
public void decodeWithString() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
byte[] result = decoder.decode(value);
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).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
encodeWithDst
Java Encoding Decoding Base64 建立一個 String ,轉成 byte[] 使用 Base64.Encoder 進行編碼,將結果存放到另一個陣列。
@Test
public void encodeWithDst() 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);
int size = 100;
byte[] dst = new byte[size];
int result = encoder.encode(value.getBytes(StandardCharsets.UTF_8.toString()), dst);
System.out.println(result);
System.out.println(new String(dst));
assertThat(new String(dst))
.containsIgnoringWhitespaces(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==")
.hasSize(size);
}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
92
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
decodeWithDst
Java Encoding Decoding Base64 建立一個 String ,轉成 byte[] 使用 Base64.Decoder 進行解碼,將結果存放到另一個陣列。
@Test
public void decodeWithDst() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
int size = 100;
byte[] dst = new byte[size];
int result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()), dst);
System.out.println(result);
System.out.println(new String(dst));
assertThat(new String(dst))
.containsIgnoringWhitespaces("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484")
.hasSize(size);
}
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
67
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
encodeWithByteBuffer
Base64 Java Encoder Decoder 建立一個 String ,轉成 byte[] 包裝成 ByteBuffer ,使用 Base64.Encoder 進行編碼,傳回 ByteBuffer 。
@Test
public void encodeWithByteBuffer() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(byteBuffer);
ByteBuffer result = encoder.encode(byteBuffer);
System.out.println(result);
String stringResult = new String(result.array(), StandardCharsets.UTF_8.toString());
System.out.println(stringResult);
assertThat(stringResult).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
java.nio.HeapByteBuffer[pos=0 lim=67 cap=67]
java.nio.HeapByteBuffer[pos=0 lim=92 cap=92]
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
decodeWithByteBuffer
Base64 Java Encoder Decoder 建立一個 String ,轉成 byte[] 包裝成 ByteBuffer ,使用 Base64.Decoder 進行解碼,傳回 ByteBuffer 。
@Test
public void decodeWithByteBuffer() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(byteBuffer);
ByteBuffer result = decoder.decode(byteBuffer);
System.out.println(result);
String stringResult = new String(result.array(), StandardCharsets.UTF_8.toString());
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
java.nio.HeapByteBuffer[pos=0 lim=92 cap=92]
java.nio.HeapByteBuffer[pos=0 lim=67 cap=67]
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
Base64_encodeBase64String
Base64 Java Encoding and Decoding Example 建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行編碼。
@Test
public void Base64_encodeBase64String() throws Exception {
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
System.out.println(value);
String result = org.apache.commons.codec.binary.Base64
.encodeBase64String(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
Base64_decodeBase64WithString
Base64 Java Encoding and Decoding Example 建立一個 String ,使用 Apache Commons Codec 套件 Base64 進行解碼。
@Test
public void Base64_decodeBase64WithString() throws Exception {
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(value);
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==
[B@6bf2d08e
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
EncodingDecodingExamplesTest.java
Base64 Java Encoder Decoder 新增單元測試,驗證 Base64 Java Encoding and Decoding Example 是否符合預期。
package org.ruoxue.java_147.base64.basic;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.junit.Test;
public class EncodingDecodingExamplesTest {
@Test
public void encodeToString() 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);
String result = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
@Test
public void decodeWithString() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
byte[] result = decoder.decode(value);
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
@Test
public void encodeWithDst() 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);
int size = 100;
byte[] dst = new byte[size];
int result = encoder.encode(value.getBytes(StandardCharsets.UTF_8.toString()), dst);
System.out.println(result);
System.out.println(new String(dst));
assertThat(new String(dst))
.containsIgnoringWhitespaces(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==")
.hasSize(size);
}
@Test
public void decodeWithDst() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
int size = 100;
byte[] dst = new byte[size];
int result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()), dst);
System.out.println(result);
System.out.println(new String(dst));
assertThat(new String(dst))
.containsIgnoringWhitespaces("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484")
.hasSize(size);
}
@Test
public void encodeWithByteBuffer() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(byteBuffer);
ByteBuffer result = encoder.encode(byteBuffer);
System.out.println(result);
String stringResult = new String(result.array(), StandardCharsets.UTF_8.toString());
System.out.println(stringResult);
assertThat(stringResult).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
@Test
public void decodeWithByteBuffer() throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(byteBuffer);
ByteBuffer result = decoder.decode(byteBuffer);
System.out.println(result);
String stringResult = new String(result.array(), StandardCharsets.UTF_8.toString());
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
@Test
public void Base64_encodeBase64String() throws Exception {
String value = "Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484";
System.out.println(value);
String result = org.apache.commons.codec.binary.Base64
.encodeBase64String(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result).isEqualTo(
"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==");
}
@Test
public void Base64_decodeBase64WithString() throws Exception {
String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0NjAsIElUIDQ4NA==";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(value);
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
}
}
心得分享
Base64 Java Encoding and Decoding Example 使用基本編碼器,將字串編碼為 Base64,首先將字串轉換為 byte 陣列,然後使用 Base64.Encoder 將 byte 陣列傳給 encode 方法,反之要解碼 Base64 編碼的字串,則使用 Base64.Decoder ,將字串傳給 decode 方法,取得 byte 陣列,提供 Base64 Java Encoder Decoder 工具類別的靜態方法,使用基本編碼器,進行編碼解碼的操作。