我研究了有关字符串和文件编码的主题。这次,我想知道是否有使用Java中使用Base64方法的示例。这是下面的代码段:
public class EncodeX{
public static void main(String args[])
{
byte[] message = "hello world";
String encoded = Base64.getEncoder().encodeToString(message);
System.out.println(encoded);
//This generates compile error. Needs to be fixed.
}
}
您在此行有一个错误:
byte[] message = "hello world";
应该是
byte[] message = "hello world".getBytes();
您可以尝试关注吗,
String base64encodedString = Base64.getEncoder()。encodeToString(“ hello world” .getBytes(“ utf-8”));System.out.println(“ Base64编码的字符串(基本):” + base64encodedString);
此外,请确保您已导入java.util.Base64
。