如何使用org.apache.commons.codec.binary.base64对Java对象进行Base64编码? [英] How to Base64 encode a Java object using org.apache.commons.codec.binary.base64?

查看:237
本文介绍了如何使用org.apache.commons.codec.binary.base64对Java对象进行Base64编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试对象序列化和Base64编码结果。它适用于Sun的lib:

I've been trying to do an object serialization and Base64 encode the result. It works with Sun's lib:

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

这很好用。但是,我想使用org.apache.commons.codec.binary.base64做同样的事情,但这不会返回相同的字符串:

This works fine. However, I would like to do the same using org.apache.commons.codec.binary.base64, but this does not return the same string:

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

使用Apache编码器实现byteArray的正确Base64编码的正确方法是什么?

What would be the correct way to achieve the correct Base64 encoding of a byteArray using Apache's encoder?

推荐答案

实际上<$> c $ c> commons-codec 您正在使用的版本和特定的Sun内部版本执行会给出相同的结果。我认为你认为他们提供了不同的版本,因为当你这样做时,你隐含地在一个数组上调用 toString()

Actually the commons-codec version and specific Sun internal version you are using do give the same results. I think you thought they were giving different versions because you are implicitly calling toString() on an array when you do:

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

绝对不会打印出数组内容。相反,它只会打印出数组引用的地址。

which is definitely does not print out the array contents. Instead, that will only print out the address of the array reference.

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

I've written the following program to test the encoders against each other. You'll see from the output below that the give the same results:

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

这篇关于如何使用org.apache.commons.codec.binary.base64对Java对象进行Base64编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆