Java Blob对象无法正常工作,即使类已序列化
我有一个被序列化的类,它将大量的数据对象转换为blob以保存到数据库中。在同一个类中,有解码方法将blob转换为实际对象。下面是对象的编码和解码代码Java Blob对象无法正常工作,即使类已序列化,java,serialization,object,blob,Java,Serialization,Object,Blob,我有一个被序列化的类,它将大量的数据对象转换为blob以保存到数据库中。在同一个类中,有解码方法将blob转换为实际对象。下面是对象的编码和解码代码 private byte[] encode(ScheduledReport schedSTDReport) { byte[] bytes = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Object
private byte[] encode(ScheduledReport schedSTDReport)
{
byte[] bytes = null;
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(schedSTDReport);
oos.flush();
oos.close();
bos.close();
//byte [] data = bos.toByteArray();
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//GZIPOutputStream out = new GZIPOutputStream(baos);
//XMLEncoder encoder = new XMLEncoder(out);
//encoder.writeObject(schedSTDReport);
//encoder.close();
bytes = bos.toByteArray();
//GZIPOutputStream out = new GZIPOutputStream(bos);
//out.write(bytes);
//bytes = bos.toByteArray();
}
catch (Exception e)
{
_log.error("Exception caught while encoding/zipping Scheduled STDReport", e);
}
decode(bytes);
return bytes;
}
/*
* Decode the report definition blob back to the
* ScheduledReport object.
*/
private ScheduledReport decode(byte[] bytes)
{
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ScheduledReport sSTDR = null;
try
{
ObjectInputStream ois = new ObjectInputStream(bais);
//GZIPInputStream in = new GZIPInputStream(bais);
//XMLDecoder decoder = new XMLDecoder(in);
sSTDR = (ScheduledReport)ois.readObject();//decoder.readObject();
//decoder.close();
}
catch (Exception e)
{
_log.error("IOException caught while decoding/unzipping Scheduled STDReport", e);
}
return sSTDR;
}
这里的问题是,我什么时候在这门课上改变其他东西
表示任何其他方法,将创建一个新的类版本,因此该类的新版本无法解码原始编码的blob对象。我传递给encode的对象也是seralized对象,但这个问题存在。任何想法都要感谢是的,Java二进制序列化非常脆弱:( 您可以向类中添加一个静态
serialVersionUID
字段,以便可以控制版本号…这应该可以防止由于添加方法而出现问题。但是添加字段时仍会遇到潜在问题。有关更多详细信息,请参阅JavaDocs
您可能想考虑使用另一个序列化格式来提供更多的控制。
< P>您可以实现,以便能够控制反序列化中序列化和预期的内容。