• 序列化和反序列化


    数据库:

    CREATE TABLE T_EXTERIOR  (
        "ID" INTEGER NOT NULL , 
         "EXTERIOR" BLOB(1048576) LOGGED NOT COMPACT NOT NULL )   

    序列化并保存到数据库(A项目):

            Exterior exterior = new Exterior();
            
            ObjectOutputStream oos;
            try {
                //生成序列化好后的文件
                oos = new ObjectOutputStream(new FileOutputStream(new File("aa.tmp")));
                oos.writeObject(exterior);
                oos.flush();
                oos.close();
                
                //读取序列化好后的文件并保存到数据库中
                InputStream is = new FileInputStream(new File("aa.tmp"));
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int size = 0;
                byte[] buffer = new byte[1024];
                while((size=is.read(buffer))!=-1){
                    out.write(buffer, 0, size);
                }
                final byte[] bt = out.toByteArray();
                
                String sql = "insert into T_EXTERIOR(ID,EXTERIOR) VALUES(?,?)";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    public void setValues(PreparedStatement ps) throws SQLException {
                        ps.setInt(1, 1);
                        ps.setBytes(2, bt);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                result = "false";
            }

    被序列化的类:(A项目)

    import java.io.Serializable;
    
    public class Exterior implements Serializable{
    
        private static final long serialVersionUID = 1L;
    
        public int add(int ys, int csd,int jg,int sf,int yf,int sd){
            return ys+csd+jg+sf+yf+sd;
        }
    }

    读取数据库中序列化好的类并反序列化:(B项目)

    String sql = "select EXTERIOR from T_EXTERIOR where id=1";
    InputStream ins = null;
    try{
        Connection con = jdbcTemplate.getDataSource().getConnection();
        Statement ps = con.createStatement();
        ResultSet rs = ps.executeQuery(sql);
        while(rs.next()){
            Blob blob = rs.getBlob("EXTERIOR");
            ins = blob.getBinaryStream();
        }
        
        ObjectInputStream ois = new ObjectInputStream(ins);
        Object o = ois.readObject();
        Method[] methods = o.getClass().getMethods();
        for(Method method : methods){
            if(method.getName().equals("add")){
                Class[] css = method.getParameterTypes();
                Object[] params = new Object[css.length];
                params[0] = ys;
                params[1] = csd;
                params[2] = jg;
                params[3] = sf;
                params[4] = yf;
                params[5] = sd;
                /**
                int index = 0;
                for(Class cs : css){
                    if(String.class == cs){
                        params[index++] = "String val is :" + index;
                    }
                }*/
                Object rt = method.invoke(o, params);
                if(rt.getClass() == Integer.class){
                    result = String.valueOf((Integer)rt);
                }else if(rt.getClass()==String.class){
                    result = (String)rt;
                }
            }
        }
    }catch (Exception e) {
        e.printStackTrace();
    }

    上面的代码会报错:

    Object o = ois.readObject();
    这行代码会去找classpath下是否有这个类,没有就会报错。

    所以B项目必须把上面那个被序列化的类也要拷贝过来,并且即使调用也是调用B项目里这个类的方法,而不是序列化之前的那个类的方法,可以通过add返回不同值得到校验。



  • 相关阅读:
    CF627A Xor Equation
    CF865C Gotta Go Fast
    HDU 2222 Keywords Search
    BZOJ 2038: [2009国家集训队]小Z的袜子(hose)
    BZOJ 3781: 小B的询问
    BZOJ 1086: [SCOI2005]王室联邦
    BZOJ 2120: 数颜色
    BZOJ 1503: [NOI2004]郁闷的出纳员
    BZOJ 3757: 苹果树
    BZOJ 1861: [Zjoi2006]Book 书架
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/4252407.html
Copyright © 2020-2023  润新知