Table of Contents
ToggleURL Encoding and Decoding using Base64 in Java
提供 URL 編碼器,與基本編碼器相同,唯一的區別是對 URL 和檔案名稱安全,使用 Base64 字母表進行編碼,輸出映射到一組字元A-Za-z0-9_- ,是一種二進位到文字的編碼方案, Java 8 套件 Base64 類別中提供了一個 URL 編碼器,可用於將字串編碼為 Base64,並將 Base64 字串解碼為普通字串, URL Java Encoding Decoding 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- base64
| +- url
| +- URLEncodingDecodingBase64Test.java
單元測試
Java Encoding Decoding URL 提供 Base64 URL 編碼器操作,將位元組編碼成 Base64 ,並將 Base64 字串解碼為普通字串。
encodeWithDst
建立一個 String ,轉成 byte[] 使用 Base64.UrlEncoder 進行編碼,將結果存放到另一個陣列。
@Test
public void encodeWithDst() throws Exception {
Base64.UrlEncoder encoder = Base64.getUrlEncoder();
String value = "https://www.ruoxue.org/java-learn/java-base64/";
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("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==
https://www.ruoxue.org/java-learn/java-base64/
64
aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==
decodeWithDst
Java Encoding Decoding URL 建立一個 String ,轉成 byte[] 使用 Base64.UrlDecoder 進行解碼,將結果存放到另一個陣列。
@Test
public void decodeWithDst() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==";
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("https://www.ruoxue.org/java-learn/java-base64/")
.hasSize(size);
}
aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==
46
https://www.ruoxue.org/java-learn/java-base64/
encodeWithByteBuffer
Java Encoding Decoding URL 建立一個 String ,轉成 byte[] 包裝成 ByteBuffer ,使用 Base64.UrlEncoder 進行編碼,傳回 ByteBuffer 。
@Test
public void encodeWithByteBuffer() throws Exception {
Base64.Encoder encoder = Base64.getUrlEncoder();
String value = "https://www.ruoxue.org/java-learn/java-base64/";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(byteBuffer);
ByteBuffer result = encoder.encode(byteBuffer);
String stringResult = new String(result.array(), StandardCharsets.UTF_8.toString());
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==");
}
java.nio.HeapByteBuffer[pos=0 lim=46 cap=46]
aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==
decodeWithByteBuffer
Java Encoding Decoding URL 建立一個 String ,轉成 byte[] 包裝成 ByteBuffer ,使用 Base64.UrlDecoder 進行解碼,傳回 ByteBuffer 。
@Test
public void decodeWithByteBuffer() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==";
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("https://www.ruoxue.org/java-learn/java-base64/");
}
java.nio.HeapByteBuffer[pos=0 lim=64 cap=64]
java.nio.HeapByteBuffer[pos=0 lim=46 cap=46]
https://www.ruoxue.org/java-learn/java-base64/
encodeToString
URL Java Encoder Decoder using Base64 建立一個 String ,轉成 byte[] ,使用 Base64.UrlEncoder 進行編碼。
@Test
public void encodeToString() throws Exception {
Base64.Encoder encoder = Base64.getUrlEncoder();
String value = "https://www.ruoxue.org/java-learn/java-base64/";
System.out.println(value);
String result = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result).isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==");
}
https://www.ruoxue.org/java-learn/java-base64/
aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==
decodeWithString
URL Java Encoder Decoder using Base64 建立一個 String ,使用 Base64.UrlEncoder 進行解碼。
@Test
public void decodeWithString() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==";
System.out.println(value);
byte[] result = decoder.decode(value);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("https://www.ruoxue.org/java-learn/java-base64/");
}
aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==
[B@6aa8ceb6
https://www.ruoxue.org/java-learn/java-base64/
Base64_encodeBase64URLSafeString
建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行編碼。
@Test
public void Base64_encodeBase64URLSafeString() throws Exception {
String value = "https://www.ruoxue.org/java-learn/java-base64/";
System.out.println(value);
String result = org.apache.commons.codec.binary.Base64
.encodeBase64URLSafeString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result).isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw");
}
https://www.ruoxue.org/java-learn/java-base64/
aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw
Base64_decodeBase64WithString
建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行解碼。
@Test
public void Base64_decodeBase64WithString() throws Exception {
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(value);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("https://www.ruoxue.org/java-learn/java-base64/");
}
aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==
[B@2b05039f
https://www.ruoxue.org/java-learn/java-base64/
URLEncodingDecodingBase64Test.java
URL Java Encoder Decoder using Base64 新增單元測試,驗證 URL Java Encoding and Decoding Example 是否符合預期。
package org.ruoxue.java_147.base64.url;
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 URLEncodingDecodingBase64Test {
@Test
public void encodeWithDst() throws Exception {
Base64.Encoder encoder = Base64.getUrlEncoder();
String value = "https://www.ruoxue.org/java-learn/java-base64/";
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("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==")
.hasSize(size);
}
@Test
public void decodeWithDst() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==";
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("https://www.ruoxue.org/java-learn/java-base64/")
.hasSize(size);
}
@Test
public void encodeWithByteBuffer() throws Exception {
Base64.Encoder encoder = Base64.getUrlEncoder();
String value = "https://www.ruoxue.org/java-learn/java-base64/";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(byteBuffer);
ByteBuffer result = encoder.encode(byteBuffer);
String stringResult = new String(result.array(), StandardCharsets.UTF_8.toString());
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==");
}
@Test
public void decodeWithByteBuffer() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==";
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("https://www.ruoxue.org/java-learn/java-base64/");
}
@Test
public void encodeToString() throws Exception {
Base64.Encoder encoder = Base64.getUrlEncoder();
String value = "https://www.ruoxue.org/java-learn/java-base64/";
System.out.println(value);
String result = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result).isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==");
}
@Test
public void decodeWithString() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==";
System.out.println(value);
byte[] result = decoder.decode(value);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("https://www.ruoxue.org/java-learn/java-base64/");
}
@Test
public void Base64_encodeBase64URLSafeString() throws Exception {
String value = "https://www.ruoxue.org/java-learn/java-base64/";
System.out.println(value);
String result = org.apache.commons.codec.binary.Base64
.encodeBase64URLSafeString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result).isEqualTo("aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw");
}
@Test
public void Base64_decodeBase64WithString() throws Exception {
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy9qYXZhLWxlYXJuL2phdmEtYmFzZTY0Lw==";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(value);
System.out.println(result);
System.out.println(new String(result));
assertThat(new String(result)).isEqualTo("https://www.ruoxue.org/java-learn/java-base64/");
}
}
心得分享
URL Java Encoding and Decoding Example 使用 URL 編碼器,將字串編碼為 Base64,首先將字串轉換為 byte 陣列,然後使用 Base64.UrlEncoder 將 byte 陣列傳給 encode 方法,反之要解碼 Base64 編碼的字串,則使用 Base64.UrlDecoder ,將字串傳給 decode 方法,取得 byte 陣列,提供 URL Java Encoder Decoder using Base64 工具類別的靜態方法,使用基本編碼器,進行編碼解碼的操作。