Java Base64 is a built-in class in the Java programming language that provides methods for encoding and decoding data using the Base64 encoding. The Base64 encoding is a way to represent binary data in an ASCII format, which can be easily transmitted over network protocols or stored in text files.

The Java Base64 class provides two main methods for encoding and decoding data:

  1. static byte[] encode(byte[] src): This method encodes the given byte array src into a Base64 encoded byte array. The encoded byte array is returned by the method.

  2. static byte[] decode(byte[] src): This method decodes the given Base64 encoded byte array src into a byte array. The decoded byte array is returned by the method.

Here is an example of encoding and decoding a string using Java Base64:

import java.util.Base64;

public class Base64Example {
    public static void main(String[] args) {
        String str = "Hello World!";

        // Encoding the string
        byte[] encoded = Base64.getEncoder().encode(str.getBytes());

        // Decoding the encoded string
        byte[] decoded = Base64.getDecoder().decode(encoded);

        System.out.println("Encoded String: " + new String(encoded));
        System.out.println("Decoded String: " + new String(decoded));
    }
}

Output:

Encoded String: SGVsbG8gV29ybGQh
Decoded String: Hello World!

In this example, we have used the Base64.getEncoder() and Base64.getDecoder() methods to get instances of the Base64 encoder and decoder respectively. We have then used the encode() method to encode the string and the decode() method to decode the encoded string.

java base64

原文地址: https://www.cveoy.top/t/topic/FoG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录