HOWTO的base64 EN code。使用org.apache.commons。codec.binary.base64一个Java对象? [英] Howto base64 encode an java object using org.apache.commons.codec.binary.base64?

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

问题描述

我一直试图做一个对象序列化和连接的base64 code的结果。它与太阳的lib:

  Bean01 bean01 =新Bean01();
bean01.setDefaultValues​​();
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
新的ObjectOutputStream(BAOS).writeObject(bean01);
的System.out.println(Base64.en code(baos.toByteArray()));

这工作正常。
不过,我想做到用org.apache.commons相同。codec.binary.base64,但

<$p$p><$c$c>System.out.println(org.apache.commons.$c$cc.binary.Base64.en$c$cBase64(baos.toByteArray()));

不返回相同的字符串。什么是实现使用阿帕奇连接codeR一个的字节数组的正确base64编码的正确方式?


解决方案

其实, commons- codeC 版本和特定的Sun内部版本,您正在使用的给出了相同的结果。我想,你以为他们提供不同的版本,因为你是隐式调用的toString()当你做一个阵列上:

<$p$p><$c$c>System.out.println(org.apache.commons.$c$cc.binary.Base64.en$c$cBase64(baos.toByteArray()));

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

我已经写了下面的程序来测试彼此的连接codeRS。您可以从下面的输出看到给出了相同的结果:

 导入了java.util.Random;公共类Base64Stuff
{
    公共静态无效的主要(字串[] args){
        随机随机=新的随机();
        字节[] = randomBytes新的字节[32];
        random.nextBytes(randomBytes);        字符串internalVersion = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.en code(randomBytes);
        字节[] = apacheBytes org.apache.commons codec.binary.Base64.en codeBase64(randomBytes)。
        字符串fromApacheBytes =新的String(apacheBytes);        的System.out.println(内部长度=+ internalVersion.length());
        的System.out.println(阿帕奇字节LEN =+ fromApacheBytes.length());
        的System.out.println(内部版本= |+ internalVersion +|);
        的System.out.println(阿帕奇字节= |+ fromApacheBytes +|);
        的System.out.println(内部平等阿帕奇字节?:+ internalVersion.equals(fromApacheBytes));
    }
}

下面是从它运行的输出:

 内部长度= 44
阿帕奇字节LEN = 44
内部版本= | Kf0JBpbxCfXutxjveYs8CXMsFpQYgkllcHHzJJsz9 + G = |
阿帕奇字节= | Kf0JBpbxCfXutxjveYs8CXMsFpQYgkllcHHzJJsz9 + G = |
内部平等阿帕奇字节?:真

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

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

This works fine. However, I would like to do the same using org.apache.commons.codec.binary.base64, but

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

does not return the same string. What would be the correct way to achieve the correct base64 encoding of an byteArray using apaches encoder?

解决方案

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));
    }
}

And here's the output from a run of it:

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

这篇关于HOWTO的base64 EN code。使用org.apache.commons。codec.binary.base64一个Java对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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