Base64 编码与解码:URL Base64、MIME Base64

Java 8 中 Base 64

java 8 在标准API中增加Base64功能,通过 java.util.Base64工具类:

Java 8 基本 Base64

输入任意字符串输出被映射到“A-Za-z0-9+/”字符集中没有回车符、换行符解码从该字符集中解析为任意字符

// 编码

String asB64 = Base64.getEncoder().encodeToString("some string".getBytes("utf-8"));

System.out.println(asB64); // 输出为: c29tZSBzdHJpbmc=

// 解码

byte[] asBytes = Base64.getDecoder().decode("c29tZSBzdHJpbmc=");

System.out.println(new String(asBytes, "utf-8")); // 输出为: some string

java 8 Base64编码没有填充情况

如果Base64编码的二进制数据不是3的倍数,输出结果使用“=”号进行填充,所以结果会有一个或两个“=”号。

解码是填充的“=”号会被丢弃。如果你去掉填充“=”,结果字符串可能不能被正确解码,可以简单使用编码没有填充方法:

String encodedString =
  Base64
.getEncoder().withoutPadding().encodeToString(originalInput.getBytes());

Java 8 URL Base64

使用URL和文件名安全Base64字母表(没有‘+’和‘/’,增加‘-’和‘_’来替换,没有回车符、换行符)

用于对url拼接参数时不确定是否有特殊字符,选择先使用base64编码先对参数进行编码,然后再拼接到url上。

 

String basicEncoded = Base64.getEncoder().encodeToString("subjects?abcd".getBytes("utf-8"));

System.out.println("Using Basic Alphabet: " + basicEncoded);

 

String urlEncoded = Base64.getUrlEncoder().encodeToString("subjects?abcd".getBytes("utf-8"));

System.out.println("Using URL Alphabet: " + urlEncoded);

// 输出为:

Using Basic Alphabet: c3ViamVjdHM/YWJjZA==

Using URL Alphabet: c3ViamVjdHM_YWJjZA==

Java 8 MIME Base64

MIME Base64编码输出被映射到“A-Za-z0-9+/”字符集中,编码输出每一行不超过76个字符,而且每行以“\r\n”符结束:

StringBuilder sb = new StringBuilder();

for (int t = 0; t < 10; ++t) {

  sb.append(UUID.randomUUID().toString());

}

 

byte[] toEncode = sb.toString().getBytes("utf-8");

String mimeEncoded = Base64.getMimeEncoder().encodeToString(toEncode);

System.out.println(mimeEncoded);

// 输出为:

NDU5ZTFkNDEtMDVlNy00MDFiLTk3YjgtMWRlMmRkMWEzMzc5YTJkZmEzY2YtM2Y2My00Y2Q4LTk5

ZmYtMTU1NzY0MWM5Zjk4ODA5ZjVjOGUtOGMxNi00ZmVjLTgyZjctNmVjYTU5MTAxZWUyNjQ1MjJj

NDMtYzA0MC00MjExLTk0NWMtYmFiZGRlNDk5OTZhMDMxZGE5ZTYtZWVhYS00OGFmLTlhMjgtMDM1

ZjAyY2QxNDUyOWZiMjI3NDctNmI3OC00YjgyLThiZGQtM2MyY2E3ZGNjYmIxOTQ1MDVkOGQtMzIz

Yi00MDg0LWE0ZmItYzkwMGEzNDUxZTIwOTllZTJiYjctMWI3MS00YmQzLTgyYjUtZGRmYmYxNDA4

Mjg3YTMxZjMxZmMtYTdmYy00YzMyLTkyNzktZTc2ZDc5ZWU4N2M5ZDU1NmQ4NWYtMDkwOC00YjIy

LWIwYWItMzJiYmZmM2M0OTBm

 

byte[] decodedBytes = Base64.getMimeDecoder().decode(mimeEncoded);

String decodedMime = new String(decodedBytes,"utf-8");

System.out.println(decodedMime);

 

java 8 Base64 流的封装

 

Base64类封装了所有的BASE64编码器和解码器,还支持流的封装,这是一个非常优雅的构造,包括编码和效率都很高(无需缓冲Buffer),即编码器和解码器的输入和输出无需缓冲Buffer。下面我们以一个例子来说明编码器是怎样封装FileOutputStream,以及解码器是怎样封装FileInputStream的,两者皆不需要缓冲Buffer:

 

public void wrapping() throws IOException {

  String src = "This is the content of any resource read from somewhere" +

    " into a stream. This can be text, image, video or any other stream.";

 

  // 编码器封装OutputStream, 文件/tmp/buff-base64.txt的内容是BASE64编码的形式

  try (OutputStream os = Base64.getEncoder().wrap(newFileOutputStream("/tmp/buff-base64.txt"))) {

    os.write(src.getBytes("utf-8"));

  }

 

  // 解码器封装InputStream, 以及以流的方式解码, 无需缓冲

  // is being consumed. There is no need to buffer the content of the file just for decoding it.

  try (InputStream is = Base64.getDecoder().wrap(newFileInputStream("/tmp/buff-base64.txt"))) {

    int len;

    byte[] bytes = new byte[100];

    while ((len = is.read(bytes)) != -1) {

      System.out.print(new String(bytes, 0, len, "utf-8"));

    }

  }

}

 

参考:

https://blog.csdn.net/chszs/article/details/17027649

https://blog.csdn.net/neweastsun/article/details/80542350

你可能感兴趣的:(基础知识,java)