添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Java 9

Use the Java 8 solution. Note DatatypeConverter can still be used, but it is now within the java.xml.bind module which will need to be included.

module org.example.foo {
    requires java.xml.bind;

Java 8

Java 8 now provides java.util.Base64 for encoding and decoding base64.

Encoding

byte[] message = "hello world".getBytes(StandardCharsets.UTF_8);
String encoded = Base64.getEncoder().encodeToString(message);
System.out.println(encoded);
// => aGVsbG8gd29ybGQ=

Decoding

byte[] decoded = Base64.getDecoder().decode("aGVsbG8gd29ybGQ=");
System.out.println(new String(decoded, StandardCharsets.UTF_8));
// => hello world

Java 6 and 7

Since Java 6 the lesser known class javax.xml.bind.DatatypeConverter can be used. This is part of the JRE, no extra libraries required.

Encoding

byte[] message = "hello world".getBytes("UTF-8");
String encoded = DatatypeConverter.printBase64Binary(message);
System.out.println(encoded);
// => aGVsbG8gd29ybGQ=  

Decoding

byte[] decoded = DatatypeConverter.parseBase64Binary("aGVsbG8gd29ybGQ=");
System.out.println(new String(decoded, "UTF-8"));
// => hello world
                I used so far the Base64 class, but now I had to run one application with Java 7. I didn't know the DatatypeConverter class so far. Thanks.
– Semaphor
                Jun 27, 2016 at 14:17
                parseBase64Binary does not work for data larger than 64k ? what to use in that case(being using java 7 only)
– dynamicJos
                Jun 21, 2019 at 5:28

Within Apache Commons, commons-codec-1.7.jar contains a Base64 class which can be used to encode.

Via Maven:

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>20041127.091804</version>
</dependency>

Direct Download

Apache Commons does indeed provide a viable Base64 decoder one can use. You might especially want to use this for Java versions earlier than 1.8, but there are much, much newer version of the library that can be used. As of this writing you could use 1.11 which is much newer than 20041127.091804 (referenced above). – jwj Apr 23, 2018 at 20:07 This is what I use since it works in Java 7. Just want to point this out to people looking for a solution that works with a lower version of Java. – user11006286 Apr 25, 2022 at 17:57 the problem is that only api 26+ run this... 8% of devices have this version in 2019... so sad. – vinicius gati Oct 3, 2019 at 17:58 @viniciusgati Base64 was added in api level 8, that pretty much means it's supported on 100% of devices in existence. – nitzanj Jan 2, 2020 at 12:12 @viniciusgati note that Android has 2 Base64 classes: android.util.Base64 (since API 8) and java.util.Base64 (since API 26). Be careful with the package name when importing it. – Andrew T. Jun 3, 2020 at 3:56 Guava is also 22k methods, which might not be a big deal for you unless you are importing a lot of other libraries as well. I recently hit this lovely wall myself (65k method limit on DEX) and after removing everything I didn't need from google play, I was only 100 methods under the limit. However, after removing guava, I regained roughly 22k methods. – alphanumeric character Nov 10, 2014 at 22:36