Base64 in Java

Opublikowano

W tym przewodniku

This developer guide on Base64Encoding explains java in plain language for developers and everyday users.

Everything important stays private: try the matching converter or tool in your browser — files and tokens are not uploaded.

Read the full English article below for code samples and edge cases, or switch language anytime from the menu. We keep expanding translations from real user feedback.

Wypróbuj potem te prywatne narzędzia

Since Java 8 the JDK includes java.util.Base64, which replaces older options such as sun.misc.BASE64Encoder (removed) and Apache Commons Codec (still fine, but no longer necessary).

Three flavours

Factory Alphabet Line breaks
Base64.getEncoder() / getDecoder() + / none; decoder rejects them
Base64.getUrlEncoder() / getUrlDecoder() - _ none
Base64.getMimeEncoder() / getMimeDecoder() + / \r\n every 76 chars; decoder ignores invalid chars

Strings

import java.nio.charset.StandardCharsets;
import java.util.Base64;

String b64 = Base64.getEncoder().encodeToString("Grüße 👋".getBytes(StandardCharsets.UTF_8));
String text = new String(Base64.getDecoder().decode(b64), StandardCharsets.UTF_8);

Always pass a charset: getBytes() without one uses the platform default, which differs between servers (before Java 18).

URL-safe without padding (JWT style)

String token = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
byte[] back = Base64.getUrlDecoder().decode(token); // padding is optional when decoding

Handling invalid input

The basic decoder throws IllegalArgumentException for characters outside the alphabet, including line breaks:

try {
    byte[] data = Base64.getDecoder().decode(input);
} catch (IllegalArgumentException e) {
    // "Illegal base64 character a" -> the hex code of the bad character
}

If the value comes from e-mail or PEM and contains newlines, use getMimeDecoder().

Files and streams

byte[] file = Files.readAllBytes(Path.of("report.pdf"));
String b64 = Base64.getEncoder().encodeToString(file);

try (OutputStream out = Base64.getEncoder().wrap(Files.newOutputStream(Path.of("big.b64")));
     InputStream in = Files.newInputStream(Path.of("big.bin"))) {
    in.transferTo(out);
}

wrap() streams data without loading everything into memory.

Android

On Android API 26+ use java.util.Base64. For older versions use android.util.Base64 with flags such as Base64.NO_WRAP; without that flag it inserts line breaks.

Test your output with the Base64 decoder or the standard detector.

Najczęściej zadawane pytania

Does this page upload my data?

No. Articles are static. Linked tools run locally in your browser unless a feature explicitly needs a network relay.

Can I request a translation or a new tool?

Yes — use the private feedback form. We prioritize what users ask for across languages.

What is the fastest way to try java?

Open the homepage Base64 converter or the related tools linked on this page and paste a sample.

Powiązane narzędzia i poradniki

Komentarze (0)

Pytania, pomysły lub błąd w „Base64 in Java”? Zostaw komentarz, oceń narzędzie lub odpowiedz innym.

Brak komentarzy. Podziel się opinią jako pierwszy!

Dodaj komentarz

Oceń to narzędzie:
Esc
↑ ↓ przejdź↵ otwórzEsc zamknij