📃
jdk
  • Introduction
  • 设计模式
  • 多线程
    • 多线程_基础篇
    • 多线程_锁
    • 多线程_JUC原子类
    • 多线程_JUC锁集合
    • 多线程_JUC集合
    • 多线程_JUC线程池
    • 多线程_生产者消费者
  • IDEA快捷键
  • Spring注解
    • Spring注解_介绍
    • Spring注解_常用注解
    • Spring注解_Spring入门篇
  • Java基础
    • Java基础_集合
    • Java基础_泛型
    • Java基础_打jar包
    • Java基础_正则匹配
    • Java基础_反射机制
    • Java基础_对象正反序列化
    • Java基础_读取配置文件
    • Java基础_解析与生成XML
    • Java基础_后台服务器开发
    • Java基础_四舍五入
  • Java进阶
    • Java进阶_反射
    • Java进阶_注解
  • MySQL
    • MySQL_基础
    • MySQL_约束
    • MySQL_多表查询
    • MySQL_事务
    • MySQL_数据库设计
  • JDBC
    • JDBC_JDBC基础
    • JDBC_JDBC连接池
    • JDBC_JDBCTemplate
  • Spark集群搭建
Powered by GitBook
On this page

Was this helpful?

  1. Java基础

Java基础_对象正反序列化

PreviousJava基础_反射机制NextJava基础_读取配置文件

Last updated 2 years ago

Was this helpful?

[TOC]


对象正反序列化

import java.io.*;
import java.util.Base64;

/**
 * 对象编码工具类
 */
public class ObjEncodeUtil {
    public static String encodeBase64(Object object) {
        if (object == null) {
            return null;
        }
        ObjectOutputStream oos = null;
        String result;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(object);
            result = Base64.getEncoder().encodeToString(bos.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static <T> T decodeBase64(String str) {
        ObjectInputStream ois = null;
        T result;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(str));
            ois = new ObjectInputStream(bis);
            result = (T) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

class Bean{
        int length;
        double width;

        public Bean() {
        }

        public Bean(int length, double width) {
            this.length = length;
            this.width = width;
        }

        public int getLength() {
            return length;
        }

        public void setLength(int length) {
            this.length = length;
        }

        public double getWidth() {
            return width;
        }

        public void setWidth(double width) {
            this.width = width;
        }
    }

Bean bean = new Bean(5,3.2);
String modelStr = ObjEncodeUtil.encodeBase64(bean);
Bean bean2 = ObjEncodeUtil.decodeBase64(modelStr);
import com.alibaba.fastjson.JSON;
ModelBean bean = new ModelBean();

String jsonResult = JSON.toJSONString(bean);
ModelBean bean2 = JSON.parseObject(jsonResult, ModelBean.class);
img