java - 我应该使用 new BASE64Encoder() 还是 Base64.getEncoder() 在 java 中对图像进行编码?

标签 java image base64

在 Java 中,我们发现类 Base64BASE64Encoder 通过

对字节执行 Base64 编码

Base64.getEncoder.encodeToString(someByteArray);

和分别:

new BASE64Encoder().encode(someByteArray);

我正在尝试对图像文件进行base64编码,这将返回一个相对较大的字符串(6k-7k字符长的base64)

是否存在使用其中一种高于另一种的惯例,是否有任何性能差异或其他原因而使用其中一种高于另一种?

最佳答案

使用Base64.getEncoder()sun 软件包(其中 sun.misc.BASE64Encoder 所在)不受支持,不鼓励使用它们。

参见Why Developers Should Not Write Programs that Call 'sun' Packages :

The sun.* packages are not part of the supported, public interface.

A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform.

Eclipse 可能已经向您显示访问限制 错误以及这些包中的大多数类 won't be accessible from Java 9 on .

Performance-wise it also seems advisable to use the new implementation ,虽然我自己没有做过任何测试:

Everything is evident from the table: appalling performance of sun.misc classes, acceptable performance of Commons Codec classes and very good performance of both iharder.net and MiG Base64 implementation. New Java 8 encoder/decoder is running faster than any other implementation (but you need Java 8...). javax.xml class turned out to be a hidden gem!

关于java - 我应该使用 new BASE64Encoder() 还是 Base64.getEncoder() 在 java 中对图像进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48288897/

相关文章:

java - AsyncTask 传递一个数组给 onProgressUpdate()

python - 使用 OpenCV python 跟踪黄色对象

android - 如何在android中将base64转换为json?

java - Java有没有通过多次调用一个函数来填充一个List的方法?

java - Eclipse 中的 LTE-CIVIL UnsatisfiedLinkError

jquery - 悬停时图像放大,div 框中的内容附加到图像

c# - 如何比较两个不同的图像,通过tcp发送差异,然后将差异与客户端上的图像合并?

java - 仅使用java自带的库将base64图像转换为实际图像

java - 在java中创建Base64字符串时内存不足?

java - 具有多个 MP3 的单个媒体播放器