gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:50:24 27 4
gpt4 key购买 nike

我需要在java中序列化一个对象。目前我正在使用 this code 来做到这一点,它的作用是:

  1. ObjectOutputStream.writeObject(obj);
  2. Base64 编码 obj

并反转该过程。

它适用于类内部的基本类型,例如 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;

最佳答案

我刚刚测试了您的代码,它工作正常。

我创建了一个名为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;
}

}

并声明了另一个名为 Serializeds.java 的文件,其中包含您的代码: 导入 java.io.; 导入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$

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

关于java - 如何使用ObjectOutputStream.writeObject()+Base64 (Java)深度序列化对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43943061/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com