如何在 Java 中将字节数组转换为 Base64?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2418485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:20:56  来源:igfitidea点击:

How do I convert a byte array to Base64 in Java?

javaarraysstring

提问by Sorantis

Okay, I know how to do it in C#.

好的,我知道如何在 C# 中做到这一点。

It's as simple as:

这很简单:

Convert.ToBase64String(byte[])
and Convert.FromBase64String(string) to get byte[] back.

How can I do this in Java?

我怎样才能在 Java 中做到这一点?

回答by Konstantin Spirin

Use:

用:

byte[] data = Base64.encode(base64str);

Encoding converts to Base64

编码转换为 Base64

You would need to reference commons codecfrom your project in order for that code to work.

您需要从项目中引用公共编解码器才能使该代码正常工作。

For java8:

对于 java8

import java.util.Base64

回答by Cory Klein

Java 8+

Java 8+

Encode or decode byte arrays:

编码或解码字节数组:

byte[] encoded = Base64.getEncoder().encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = Base64.getDecoder().decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

或者,如果您只想要字符串:

String encoded = Base64.getEncoder().encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.getDecoder().decode(encoded.getBytes()));
println(decoded)    // Outputs "Hello"

For more info, see Base64.

有关详细信息,请参阅Base64

Java < 8

Java < 8

Base64 is not bundled with Java versions less than 8. I recommend using Apache Commons Codec.

Base64 不与低于 8 的 Java 版本捆绑在一起。我建议使用Apache Commons Codec

For direct byte arrays:

对于直接字节数组:

Base64 codec = new Base64();
byte[] encoded = codec.encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = codec.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

或者,如果您只想要字符串:

Base64 codec = new Base64();
String encoded = codec.encodeBase64String("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(codec.decodeBase64(encoded));
println(decoded)    // Outputs "Hello"

Android (with Java < 8)

安卓(Java < 8)

If you are using the Android SDK before Java 8 then your best option is to use the bundled android.util.Base64.

如果您使用的是 Java 8 之前的 Android SDK,那么您最好的选择是使用捆绑的android.util.Base64.

For direct byte arrays:

对于直接字节数组:

byte[] encoded = Base64.encode("Hello".getBytes());
println(new String(encoded))    // Outputs "SGVsbG8="

byte [] decoded = Base64.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

或者,如果您只想要字符串:

String encoded = Base64.encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.decode(encoded));
println(decoded)    // Outputs "Hello"

回答by Ton Snoei

Additionally, for our Android friends (API Level 8):

此外,对于我们的 Android 朋友(API 级别 8):

import android.util.Base64

...

Base64.encodeToString(bytes, Base64.DEFAULT);

回答by Ihsan Izwer

In case you happen to be using Spring framework along with java, there is an easy way around.

如果您碰巧将 Spring 框架与 java 一起使用,有一个简单的方法。

  1. Import the following.

    import org.springframework.util.Base64Utils;
  2. Convert like this.

    byte[] bytearr ={0,1,2,3,4};
    String encodedText = Base64Utils.encodeToString(bytearr);
    

    To decode you can use the decodeToString method of the Base64Utils class.

  1. 导入以下内容。

    import org.springframework.util.Base64Utils;
  2. 像这样转换。

    byte[] bytearr ={0,1,2,3,4};
    String encodedText = Base64Utils.encodeToString(bytearr);
    

    要解码,您可以使用 Base64Utils 类的 decodeToString 方法。