Java 8 Base64 编码和解码示例

x33g5p2x  于9个月前 转载在 Java  
字(5.8k)|赞(0)|评价(0)|浏览(70)

在本指南中,我们将探索 Java 8 Base64 实用程序类,用于获取 Base64 编码方案的编码器和解码器。 Base64packagejava.util 下的实用程序类。

1. Java 8

编码后的字符串被映射到 A-Za-z0-9+/ 中的一组字符。编码器不添加任何换行(行分隔符)字符。解码器拒绝包含 A-Za-z0-9+/ 之外的字符的数据。

1.1 Base64 编码

String originalString = "Stay Home Stay Safe";
System.out.println("Original String: " + originalString);


String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());
System.out.println("Encoded String: " + encodedString);

1.2 Base64 解码

byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
String decodedString = new String(decodedBytes);
System.out.println("Decoded String: " + decodedString);

输出

Original String: Stay Home Stay Safe
Encoded String: U3RheSBIb21lIFN0YXkgU2FmZQ==
Decoded String: Stay Home Stay Safe

2. Java 8- URL 和文件名

编码后的字符串被映射到 A-Za-z0-9+_ 中的一组字符。解码器拒绝包含 A-Za-z0-9+_ 之外的字符的数据。

2.1 URL 编码

String originalURL = "https://www.google.com/search?ei=KIyJXtq4KOic4-EP_P-10A0&q=websparrow.org";
System.out.println("Original URL String: " + originalURL);


String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes());
System.out.println("Encoded URL String: " + encodedURL);

2.2 URL 解码

byte[] decodedURLBytes = Base64.getUrlDecoder().decode(encodedURL);
String decodedURL = new String(decodedURLBytes);
System.out.println("Decoded URL String: " + decodedURL);

输出

Original URL String: https://www.google.com/search?ei=KIyJXtq4KOic4-EP_P-10A0&q=websparrow.org
Encoded URL String: aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g_ZWk9S0l5Slh0cTRLT2ljNC1FUF9QLTEwQTAmcT13ZWJzcGFycm93Lm9yZw==
Decoded URL String: https://www.google.com/search?ei=KIyJXtq4KOic4-EP_P-10A0&q=websparrow.org

3. Java 8- MIME

编码输出必须以每行不超过 76 个字符的行表示,并使用回车 ‘\r’ 后跟换行符**‘\n’** 作为行分隔符。编码输出的末尾不存在行分隔符。

3.1 MIME 编码

StringBuilder originalMimeString = new StringBuilder();
for (int ctr = 0; ctr < 10; ctr++) {
	originalMimeString.append(UUID.randomUUID().toString());
}

System.out.println("Original MIME String: " + originalMimeString);


String encodedMime = Base64.getMimeEncoder()
		.encodeToString(originalMimeString.toString().getBytes());
System.out.println("Encoded MIME String: " + encodedMime);

3.2 MIME 解码

byte[] decodedMimeBytes = Base64.getMimeDecoder().decode(encodedMime);
String decodedMime = new String(decodedMimeBytes);
System.out.println("Decoded MIME String: " + decodedMime);

输出

Original MIME String: 46b1778d-d8be-4cb6-9b80-8d439bfd443321d9e6a1-aae2-482e-81e5-742f85e5a1187694bd39-c051-42aa-a990-ae638d8b5bc2fc07cfae-1b82-412f-ba0a-1b7afa88e7882ec0bd15-4d70-45b3-83dc-7ae0b97856c3f2c7400e-b447-4630-bb14-622174091b9fe4e6fdbf-e218-4dcc-8d8b-c65f0f6079584a9eb92e-436e-444b-80f3-89583b96524ccaa48e6c-94c9-4d9d-9b40-676fc488d01d3560a4c4-9e9e-4a0c-a121-d986df398e11
Encoded MIME String: NDZiMTc3OGQtZDhiZS00Y2I2LTliODAtOGQ0MzliZmQ0NDMzMjFkOWU2YTEtYWFlMi00ODJlLTgx
ZTUtNzQyZjg1ZTVhMTE4NzY5NGJkMzktYzA1MS00MmFhLWE5OTAtYWU2MzhkOGI1YmMyZmMwN2Nm
YWUtMWI4Mi00MTJmLWJhMGEtMWI3YWZhODhlNzg4MmVjMGJkMTUtNGQ3MC00NWIzLTgzZGMtN2Fl
MGI5Nzg1NmMzZjJjNzQwMGUtYjQ0Ny00NjMwLWJiMTQtNjIyMTc0MDkxYjlmZTRlNmZkYmYtZTIx
OC00ZGNjLThkOGItYzY1ZjBmNjA3OTU4NGE5ZWI5MmUtNDM2ZS00NDRiLTgwZjMtODk1ODNiOTY1
MjRjY2FhNDhlNmMtOTRjOS00ZDlkLTliNDAtNjc2ZmM0ODhkMDFkMzU2MGE0YzQtOWU5ZS00YTBj
LWExMjEtZDk4NmRmMzk4ZTEx
Decoded MIME String: 46b1778d-d8be-4cb6-9b80-8d439bfd443321d9e6a1-aae2-482e-81e5-742f85e5a1187694bd39-c051-42aa-a990-ae638d8b5bc2fc07cfae-1b82-412f-ba0a-1b7afa88e7882ec0bd15-4d70-45b3-83dc-7ae0b97856c3f2c7400e-b447-4630-bb14-622174091b9fe4e6fdbf-e218-4dcc-8d8b-c65f0f6079584a9eb92e-436e-444b-80f3-89583b96524ccaa48e6c-94c9-4d9d-9b40-676fc488d01d3560a4c4-9e9e-4a0c-a121-d986df398e11

请参阅完整示例。

Base64EncoderDecoder.java

package org.websparrow;

import java.util.Base64;
import java.util.UUID;

public class Base64EncoderDecoder {

	public static void main(String[] args) {

		System.out.println("============================");
		System.out.println("Basic Encoding and Decoding");
		System.out.println("============================");

		// Basic/ Simple Types
		String originalString = "Stay Home Stay Safe";
		System.out.println("Original String: " + originalString);

		// Basic Encoding
		String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());
		System.out.println("Encoded String: " + encodedString);

		// Basic Decoding
		byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
		String decodedString = new String(decodedBytes);
		System.out.println("Decoded String: " + decodedString);

		System.out.println("\n==================================");
		System.out.println("URL/Filename Encoding and Decoding");
		System.out.println("==================================");

		// URL or Filename types
		String originalURL = "https://www.google.com/search?ei=KIyJXtq4KOic4-EP_P-10A0&q=websparrow.org";
		System.out.println("Original URL String: " + originalURL);

		// URL/Filename Encoding
		String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes());
		System.out.println("Encoded URL String: " + encodedURL);

		// URL/Filename Decoding
		byte[] decodedURLBytes = Base64.getUrlDecoder().decode(encodedURL);
		String decodedURL = new String(decodedURLBytes);
		System.out.println("Decoded URL String: " + decodedURL);

		System.out.println("\n==========================");
		System.out.println("MIME Encoding and Decoding");
		System.out.println("==========================");

		// MIME types
		StringBuilder originalMimeString = new StringBuilder();
		for (int ctr = 0; ctr < 10; ctr++) {
			originalMimeString.append(UUID.randomUUID().toString());
		}

		System.out.println("Original MIME String: " + originalMimeString);

		// MIME Encoding
		String encodedMime = Base64.getMimeEncoder()
				.encodeToString(originalMimeString.toString().getBytes());
		System.out.println("Encoded MIME String: " + encodedMime);

		// MIME Decoding
		byte[] decodedMimeBytes = Base64.getMimeDecoder().decode(encodedMime);
		String decodedMime = new String(decodedMimeBytes);
		System.out.println("Decoded MIME String: " + decodedMime);

	}
}

相关文章

热门文章

更多