本文介绍了Java-方法encodeBase64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串String x = "Sample text";,我想打印它的base64加密.正如无数示例提到的那样,我使用:

I have a String String x = "Sample text"; and I want to print the base64 encryption of it. As numerous examples mention, I use:

byte[] encodedBytes = Base64.encodeBase64(x.getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));

但这给了我The method encodeBase64(byte[]) is undefined for the type Base64 ...那是为什么?

But this gives me The method encodeBase64(byte[]) is undefined for the type Base64... Why is that?

推荐答案

encode方法在Base64.Encoder类中,您可以通过运行Base64.getEncoder()来获取.

The encode method is in the Base64.Encoder class that you can get by running Base64.getEncoder().

byte[] encodedBytes = Base64.getEncoder().encode(x.getBytes());

类似地,要解码:

String originalString = new String(Base64.getDecoder().decode(encodedBytes));

签出 Base64 javadocs 了解更多信息.

Check out the Base64 javadocs for more info.

这篇关于Java-方法encodeBase64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 18:27