序列化和反序列化

数据库:

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返回不同值得到校验。



 

posted @ 2015-01-27 11:35  自行车上的程序员  阅读(331)  评论(0编辑  收藏  举报