Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

java - 在Java中编码为Base64(Encoding as Base64 in Java)

I need to encode some data in the Base64 encoding in Java.

(我需要使用Java中的Base64编码对一些数据进行编码。)

How do I do that?

(我怎么做?)

What is the name of the class that provides a Base64 encoder?

(提供Base64编码器的类的名称是什么?)


I tried to use the sun.misc.BASE64Encoder class, without success.

(我尝试使用sun.misc.BASE64Encoder类,但没有成功。)

I have the following line of Java 7 code:

(我有以下几行Java 7代码:)

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

I'm using Eclipse.

(我正在使用Eclipse。)

Eclipse marks this line as an error.

(Eclipse将此行标记为错误。)

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:

(我使用Apache Commons作为建议的解决方案,包括:)

import org.apache.commons.*;

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

(并导入从以下网址下载的JAR文件: http//commons.apache.org/codec/)

But the problem still exists.

(但是问题仍然存在。)

Eclipse still shows the errors previously mentioned.

(Eclipse仍然显示前面提到的错误。)

What should I do?

(我该怎么办?)

  ask by Jury A translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(然后更改您的类以使用Base64类。)

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 .

(然后阅读为什么不应该使用sun。*软件包 。)


Update (2016-12-16) (更新(2016-12-16))

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

(现在,您可以将java.util.Base64与Java 8一起使用。首先,像平常一样导入它:)

import java.util.Base64;

Then use the Base64 static methods as follows:

(然后使用Base64静态方法,如下所示:)

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 Java documentation for Base64 for more.

(有关更多信息,请参见Base64的Java文档 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...