首页 > 学院 > 开发设计 > 正文

序列化和反序列化

2019-11-11 05:20:46
字体:
来源:转载
供稿:网友
package com.****.web.util;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/** * 序列化对象Util * */public final class SerializeUtil { /** * 序列化对象 * @param object * @return * @throws IOException */ public static String serializeObject(Object object) throws IOException{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(object); out.flush(); out.close(); //加密 return new BASE64Encoder().encode(baos.toByteArray()); } /** * 反序列化对象 * @param buf * @return * @throws IOException * @throws ClassNotFoundException */ public static Object deserializeObject(String buf) throws IOException, ClassNotFoundException{ Object object = null; ByteArrayInputStream bais = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(buf)); //解码 ObjectInputStream in = new ObjectInputStream(bais); object = in.readObject(); in.close(); return object; } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表