小编mat*_*ger的帖子

使用不同的包名称对不同应用程序中的java对象进行de/serialize

我想在不同的应用程序之间共享java对象.

只要我在不同的项目中使用相同的包名称,它就可以正常工作.但是,如果我更改包名称,它就不再起作用了.

我试图通过扩展ObjectInputStream类并重写readClassDescriptor方法来解决这个问题.

但通过这样做,我收到以下错误:

java.io.StreamCorruptedException: invalid type code: 00
Run Code Online (Sandbox Code Playgroud)

......不知道如何解决这个问题.

这是我用于扩展ObjectInputStream类的代码:

public class MyObjectInputStream extends ObjectInputStream {

    public static Map<String, Class> classNameMapping = initclassNameMapping(); 

    private static Map<String, Class> initclassNameMapping(){
        Map<String, Class> res = new HashMap<String, Class>();
        //ipxTest is the name of the package where the objects got serialized
        res.put("ipxTest.IPX", interprojectxchangeTest.IPX.class); 
        res.put("ipxTest.A", interprojectxchangeTest.A.class);
        return Collections.unmodifiableMap(res);
    }

    public MyObjectInputStream(InputStream in) throws IOException {
        super(in);
        enableResolveObject(true);
    }


    protected MyObjectInputStream() throws IOException, SecurityException {
        super();
        enableResolveObject(true);
    }

    @Override
    protected java.io.ObjectStreamClass …
Run Code Online (Sandbox Code Playgroud)

java serializable

4
推荐指数
1
解决办法
2600
查看次数

标签 统计

java ×1

serializable ×1