Base64 Encoding in Java

2018-12-31 03:33发布

I'm using Eclipse. I have the following line of code:

wr.write(new sun.misc.BASE64Encoder().encode(buf));

Eclipse marks this line as an error. I imported the required libraries:

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

But again, both of them are shown as errors. I found a similar post here.

I used Apache Commons as the solution suggested by including:

import org.apache.commons.*;

and importing the JAR files downloaded from: http://commons.apache.org/codec/

But the problem still exists. Eclipse still shows the errors previously mentioned; please advise.

标签: java base64
16条回答
公子世无双
2楼-- · 2018-12-31 03:57

In Java 8 it can be done as

Base64.getEncoder().encodeToString(string.getBytes(StandardCharsets.UTF_8))

Here is a short, self-contained complete example

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

public class Temp {
    public static void main(String... args) throws Exception {
        final String s = "old crow medicine show";
        final byte[] authBytes = s.getBytes(StandardCharsets.UTF_8);
        final String encoded = Base64.getEncoder().encodeToString(authBytes);
        System.out.println(s + " => " + encoded);
    }
}

gives output

old crow medicine show => b2xkIGNyb3cgbWVkaWNpbmUgc2hvdw==
查看更多
临风纵饮
3楼-- · 2018-12-31 03:59

Eclipse gives you an error/warning because you are trying to use internal classes that are specific to a JDK vendor and not part of the public API. Jakarta Commons provides its own implementation of base64 codecs, which of course reside in a different package. Delete those imports and let Eclipse import the proper Commons classs for you.

查看更多
初与友歌
4楼-- · 2018-12-31 04:00

If you are using Spring framework at least version 4.1, you can use org.springframework.util.Base64Utils class:

byte[] raw = { 1, 2, 3 };
String encoded = Base64Utils.encodeToString(raw);
byte[] decoded = Base64Utils.decodeFromString(encoded);

It will delegate to Java 8's Base64, Apache Commons Codec or JAXB DatatypeConverter, depending on what is available.

查看更多
爱死公子算了
5楼-- · 2018-12-31 04:02

I tried with the following code snippet. It worked well. :-)

com.sun.org.apache.xml.internal.security.utils.Base64.encode("The string to encode goes here");
查看更多
明月照影归
6楼-- · 2018-12-31 04:04

You need to change the import of your Class:

import org.apache.commons.codec.binary.Base64;

And then change your Class to use the Base64 class.

Here's some example code:

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

Then read why you shouldn't use sun.* packages.


Update (16/12/2016)

You can now java.util.Base64 with Java8. First, import it as you normally do:

import java.util.Base64;

Then use the Base64 static methods as follows:

byte[] encodedBytes = Base64.getEncoder().encode("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));

If you directly want to encode string and get the result as encoded string you can use this.

String encodeBytes = Base64.getEncoder().encodeToString((userName + password).getBytes());

See Javadocs for Base64 for more: https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html

查看更多
笑指拈花
7楼-- · 2018-12-31 04:04

If you are stuck to an earlier version of Java than 8 but already using AWS SDK for Java, you can use com.amazonaws.util.Base64.

查看更多
登录 后发表回答