简体   繁体   English

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

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

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.

Java 8+ Java 8+<\/h1>

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"<\/code><\/pre>

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"<\/code><\/pre>

For more info, seeBase64<\/a> .有关详细信息,请参阅Base64<\/a> 。

Java < 8爪哇 < 8<\/h1>

Base64 is not bundled with Java versions less than 8. I recommend using Apache Commons Codec<\/a> . Base64 不与低于 8 的 Java 版本捆绑在一起。我建议使用Apache Commons Codec<\/a> 。

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"<\/code><\/pre>

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"<\/code><\/pre>

Spring春天<\/h1>

If you're working in a Spring project already, you may find their org.springframework.util.Base64Utils<\/code><\/a> class more ergonomic:如果您已经在 Spring 项目中工作,您可能会发现他们的org.springframework.util.Base64Utils<\/code><\/a>类更符合人体工程学:

For direct byte arrays:对于直接字节数组:

 byte[] encoded = Base64Utils.encode("Hello".getBytes()); println(new String(encoded)) \/\/ Outputs "SGVsbG8=" byte[] decoded = Base64Utils.decode(encoded); println(new String(decoded)) \/\/ Outputs "Hello"<\/code><\/pre>

Or if you just want the strings:或者,如果您只想要字符串:

 String encoded = Base64Utils.encodeToString("Hello".getBytes()); println(encoded); \/\/ Outputs "SGVsbG8=" String decoded = Base64Utils.decodeFromString(encoded); println(new String(decoded)) \/\/ Outputs "Hello"<\/code><\/pre>

Android (with Java < 8) Android(Java < 8)<\/h1>

If you are using the Android SDK before Java 8 then your best option is to use the bundled android.util.Base64<\/code> .如果您在 Java 8 之前使用 Android SDK,那么您最好的选择是使用捆绑的android.util.Base64<\/code> 。

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"<\/code><\/pre>

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"<\/code><\/pre>"

Use:利用:

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

Additionally, for our Android friends (API Level 8):此外,对于我们的 Android 朋友(API 级别 8):

import android.util.Base64

...

Base64.encodeToString(bytes, Base64.DEFAULT);

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;导入 org.springframework.util.Base64Utils;<\/pre><\/li>
  2. Convert like this.像这样转换。

    \nbyte[] bytearr ={0,1,2,3,4};字节 [] 字节泪 = {0,1,2,3,4};\nString encodedText = Base64Utils.encodeToString(bytearr);字符串编码文本 = Base64Utils.encodeToString(bytearr);\n<\/pre>

    To decode you can use the decodeToString method of the Base64Utils class.要解码,您可以使用 Base64Utils 类的 decodeToString 方法。

    <\/li><\/ol>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM