English 中文(简体)
如何使用 org. apache. commons. codec. binary. base64 编码 Java 对象?
原标题:How to Base64 encode a Java object using org.apache.commons.codec.binary.base64?

我一直在尝试做一个物体序列化和 Base64 编码结果。 它与Sun Sulb合作:

Bean01 bean01 = new Bean01();
bean01.setDefaultValues();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new ObjectOutputStream( baos ).writeObject( bean01 );
System.out.println(Base64.encode(baos.toByteArray()));

不过,我想用Ong.apache.commons.codec.binary. base64 来做同样的事情,但这并不返回相同的字符串:

System.out.println (org.apache.commons.codec.binary.Base64.encodeBase64 (baos.toByteAray ()); (org.apache.commons.codec.binary.Base64.encodeBase64 (baos.toByteAray ());

用阿帕奇编码器对字节阵列进行正确的 Base64 编码的正确方式是什么?

最佳回答

实际上,您正在使用的 < commons- codec 版本和具体的 Sun 内部版本的 < strong > do 给与同样的结果。 我想你认为他们给出不同的版本, 是因为你暗中将 < code> to String () 调用在一个阵列上, 当你这样做时:

System.out.println(org.apache.commons.codec.binary.Base64.encodeBase64(baos.toByteArray()));

其中肯定不打印数组内容。相反,这将只打印数组引用的地址。

我写了以下程序来测试编码器相互对立。 您可以从下面的输出中看到结果相同:

import java.util.Random;

public class Base64Stuff
{
    public static void main(String[] args) {
        Random random = new Random();
        byte[] randomBytes = new byte[32];
        random.nextBytes(randomBytes);

        String internalVersion = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(randomBytes);
        byte[] apacheBytes =  org.apache.commons.codec.binary.Base64.encodeBase64(randomBytes);
        String fromApacheBytes = new String(apacheBytes);

        System.out.println("Internal length = " + internalVersion.length());
        System.out.println("Apache bytes len= " + fromApacheBytes.length());
        System.out.println("Internal version = |" + internalVersion + "|");
        System.out.println("Apache bytes     = |" + fromApacheBytes + "|");
        System.out.println("internal equal apache bytes?: " + internalVersion.equals(fromApacheBytes));
    }
}

这里显示它运行的输出 :

Internal length = 44
Apache bytes len= 44
Internal version = |Kf0JBpbxCfXutxjveYs8CXMsFpQYgkllcHHzJJsz9+g=|
Apache bytes     = |Kf0JBpbxCfXutxjveYs8CXMsFpQYgkllcHHzJJsz9+g=|
internal equal apache bytes?: true
问题回答

http://commons.apache.org/codec/"rel="no follow">commons-codec主页:

Codec was formed as an attempt to focus development effort on one definitive implementation of the Base64 encoder. At the time of Codec s proposal, there were approximately 34 different Java classes that dealt with Base64 encoding spread over the Foundation s CVS repository. Developers in the Jakarta Tomcat project had implemented an original version of the Base64 codec which had been copied by the Commons HttpClient and Apache XML project s XML-RPC subproject. After almost one year, the two forked versions of Base64 had significantly diverged from one another. XML-RPC had applied numerous fixes and patches which were not applied to the Commons HttpClient Base64. Different subprojects had differing implementations at various levels of compliance with the RFC 2045.

我认为你的问题是"不同层次"的遵守

我的建议: 选择一个基数64 编码器/编码器, 并坚持它





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签