Java has always lacked the BASE64 encoding API, so that third-party API implementations are usually used in project development. However, Java 8 implements the BASE64 codec API, which is included in the java.util package.
The java.util.Base64 tool class provides a set of static methods to obtain the following three BASE64 codecs:
1) Basic encoding
2) URL encoding
3) MIME encoding
Basic encoding is the standard BASE64 encoding, which is used to handle general requirements: the output content does not add newlines, and the output content consists of letters and numbers.
// encode String asB64 = Base64.getEncoder().encodeToString("some string".getBytes("utf-8")); System.out.println(asB64); // The output is: c29tZSBzdHJpbmc= // decode byte[] asBytes = Base64.getDecoder().decode("c29tZSBzdHJpbmc="); System.out.println(new String(asBytes, "utf-8")); // 输出为: some string
URL encoding is also a requirement we often face, but because URL has a special meaning for the backslash "/", URL encoding needs to replace it and replace it with an underscore.
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); // output is: Using Basic Alphabet: c3ViamVjdHM/YWJjZA== Using URL Alphabet: c3ViamVjdHM_YWJjZA==
MIME encoding produces BASE64 output using basic alphanumerics, and is MIME friendly: each line of output does not exceed 76 characters, and each line ends with a "\r\n" character.
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); // output is: NDU5ZTFkNDEtMDVlNy00MDFiLTk3YjgtMWRlMmRkMWEzMzc5YTJkZmEzY2YtM2Y2My00Y2Q4LTk5 ZmYtMTU1NzY0MWM5Zjk4ODA5ZjVjOGUtOGMxNi00ZmVjLTgyZjctNmVjYTU5MTAxZWUyNjQ1MjJj NDMtYzA0MC00MjExLTk0NWMtYmFiZGRlNDk5OTZhMDMxZGE5ZTYtZWVhYS00OGFmLTlhMjgtMDM1 ZjAyY2QxNDUyOWZiMjI3NDctNmI3OC00YjgyLThiZGQtM2MyY2E3ZGNjYmIxOTQ1MDVkOGQtMzIz Yi00MDg0LWE0ZmItYzkwMGEzNDUxZTIwOTllZTJiYjctMWI3MS00YmQzLTgyYjUtZGRmYmYxNDA4 Mjg3YTMxZjMxZmMtYTdmYy00YzMyLTkyNzktZTc2ZDc5ZWU4N2M5ZDU1NmQ4NWYtMDkwOC00YjIy LWIwYWItMzJiYmZmM2M0OTBm
The java.util.Base64 class encapsulates all BASE64 encoders and decoders, and also supports the encapsulation of streams - this is a very elegant construct - including encoding and efficient (no need to buffer Buffer) - the encoder and decoder input and output without buffering Buffer.
Let's take an example to illustrate how the encoder encapsulates FileOutputStream and the decoder encapsulates FileInputStream, both without buffering:
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."; // The encoder encapsulates OutputStream, and the content of the file /tmp/buff-base64.txt is in the form of BASE64 encoding try (OutputStream os = Base64.getEncoder().wrap(newFileOutputStream("/tmp/buff-base64.txt"))) { os.write(src.getBytes("utf-8")); } // The decoder encapsulates the InputStream and decodes it as a stream without buffering // 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")); } } }
Transfer: http://blog.csdn.net/chszs/article/details/17027649