要将Java 8中的Base64编码解码为字符串,可以使用以下代码:

import java.util.Base64;

public class Base64DecodeToString {
    public static void main(String[] args) {
        String base64String = 'SGVsbG8gV29ybGQ=';

        // 解码为字节数组
        byte[] decodedBytes = Base64.getDecoder().decode(base64String);

        // 将字节数组转换为String
        String decodedString = new String(decodedBytes);

        System.out.println(decodedString);
    }
}

在这个例子中,我们使用Base64.getDecoder()方法获取Base64解码器,然后使用decode()方法将Base64字符串解码为字节数组。最后,使用new String()构造函数将字节数组转换为字符串。

输出将是:'Hello World'。


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

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