如何使用ObjectOutputStream.writeObject()+ Base64(Java)深度序列化对象?

时间:2017-05-12 17:05:37

标签: java serialization deserialization deep-copy

我需要在java中序列化一个Object。目前我正在使用this code执行此操作,其中包含:

  1. ObjectOutputStream.writeObject(OBJ);
  2. Base64编码obj
  3. 并扭转过程。

    它适用于类中的原始类型,例如longint

    但是,该对象中的所有String个对象都会变为null我也需要包含它们。有没有办法做到这一点?

    编辑:我正在使用的代码

    // Modified from source: https://stackoverflow.com/questions/134492/how-to-serialize-an-object-into-a-string
    public static Object FromString( String s )
    {
        Object o = null;
        try {
            byte[] data = Base64.getDecoder().decode(s);
            ObjectInputStream ois = new ObjectInputStream(
                    new ByteArrayInputStream(data));
            o = ois.readObject();
            ois.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return o;
    }
    
    // Modified from source: https://stackoverflow.com/questions/134492/how-to-serialize-an-object-into-a-string
    public static String ToString( Serializable o )
    {
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(o);
            oos.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    
        return Base64.getEncoder().encodeToString(baos.toByteArray());
    }
    

    编辑:Pojo

    public class SignedTimestamp implements Serializable {
    
        private Long obj;
        private byte[] signature;
        private String signatureAsAString;
    }
    

    另一个应用程序的输出(接收数据和反序列化):

      

    obj = 1494609033621;

         

    signature = null;

         

    signatureAsAString = null;

2 个答案:

答案 0 :(得分:1)

我在该代码中没有看到任何问题。请发布您的代码,以便我们进一步挖掘。

从我的结尾尝试:

编码序列化版本 rO0ABXNyAAhFbXBsb3llZTLR4JLRYAw9AgAESQAGbnVtYmVyTAAHYWRkcmVzc3QAEkxqYXZhL2xhbmcvU3RyaW5nO0wADWFkZHJlc3NPYmplY3R0AAlMQWRkcmVzcztMAARuYW1lcQB + AAF4cAAAAGV0ABlQaG9ra2EgS3VhbiwgQW1iZWh0YSBQZWVyc3IAB0FkZHJlc3MkcEtPHXHTqQIAAUwACGFkZHJMaW5lcQB + AAF4cHEAfgAEdAAJUmV5YW4gQWxp

重组对象 员工[姓名= Reyan Ali,地址= Phokka Kuan,Ambehta Peer,数字= 101,地址对象=地址[addrLine = Phokka Kuan,Ambehta Peer]]

答案 1 :(得分:1)

我刚刚测试了你的代码,它运行正常。

我创建了一个名为 SignedTimeStamp.java的源

import java.io.*;

public class SignedTimestamp implements Serializable {
    private Long obj;
    private byte[] signature;
    private String signatureAsAString;

    public SignedTimestamp(Long obj, byte[] signature, String signatureAsAString) {
        this.obj = obj;
        this.signature = signature;
        this.signatureAsAString = signatureAsAString;
    }

    public Long getObj() {
        return this.obj;
    }

    public byte[] getSignature() {
        return this.signature;
    }

    public String getSignatureAsAString() {
        return this.signatureAsAString;
    }

}

并声明另一个名为 Serializables.java ,其中包含您的代码:     import java.io. ;     import java.util。;

public class Serializables {
    public static Object FromString(String s) {
        Object o = null;
        try {
            byte[] data = Base64.getDecoder().decode(s);
            ObjectInputStream ois = new ObjectInputStream(
                    new ByteArrayInputStream(data));
            o = ois.readObject();
            ois.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return o;
    }

    // Modified from source: http://stackoverflow.com/questions/134492/how-to-serialize-an-object-into-a-string
    public static String ToString( Serializable o ) {
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(o);
            oos.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }

        return Base64.getEncoder().encodeToString(baos.toByteArray());
    }
}

然后,我在 Serialize.java 中创建了一个主类:

public class Serialize {

    public static void main(String[] args) throws Exception {
        SignedTimestamp o = new SignedTimestamp(100L, new byte[]{ (byte) 128 }, "Hello, world!");
        System.out.println(Serializables.ToString(o));
    }
}

这让我得到了这个结果:

sh-4.3$ java Serialize
rO0ABXNyAA9TaWduZWRUaW1lc3RhbXCGTHiJ+JenzgIAA0wAA29ianQAEExqYXZhL2xhbmcvTG9uZztbAAlzaWduYXR1cmV0AAJbQkwAEnNpZ25hdHVyZUFzQVN0cmluZ3QAEkxqYXZhL2xhbmcvU3RyaW5nO3hwc3IADmphdmEubGFuZy
5Mb25nO4vkkMyPI98CAAFKAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAAAAABkdXIAAltCrPMX+AYIVOACAAB4cAAAAAGAdAANSGVsbG8sIHdvcmxkIQ==                                    
sh-4.3$ 

然后,我在 Deserialize.java 中创建了另一个主类:

public class Deserialize {

    public static void main(String[] args) throws Exception {
        String serialized = 
            "rO0ABXNyAA9TaWduZWRUaW1lc3RhbXCGTHiJ+JenzgIAA0wAA29ianQAEExqYXZhL2xhbmcvTG9uZztbAAlzaWduYXR1cmV0AAJbQkwAEnNpZ25hdHVyZUFzQVN0cmluZ3QAEkxqYXZhL2xhbmcvU3RyaW5nO3hwc3IADmphdmEubGFuZy" +
            "5Mb25nO4vkkMyPI98CAAFKAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAAAAABkdXIAAltCrPMX+AYIVOACAAB4cAAAAAGAdAANSGVsbG8sIHdvcmxkIQ==";
        SignedTimestamp o = (SignedTimestamp) Serializables.FromString(serialized);

        System.out.println(o.getObj());
        System.out.println(o.getSignature());
        System.out.println(o.getSignatureAsAString());
    }

}

然后它返回:

sh-4.3$ java Deserialize                                                                                                                                                          
100                                                                                                                                                                               
[B@6bc7c054                                                                                                                                                                       
Hello, world!                                                                                                                                                                     
sh-4.3$ 

所以,总而言之,代码似乎完美无缺。 你是如何测试你的代码的?也许错误就在那里。