工具类

Base64.java

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable; @SuppressWarnings("restriction")
public class Base64 {
/**
* str 编码为base64
*
* @param String s
* @return String base64
* */
public static String getBase64(String s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
} /**
* byte[] 编码为base64
*
* @param byte[] ba
* @return String base64
* */
public static String getBase64(byte[] ba) {
if (ba == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(ba);
} /**
* base64 解码为 byte[]
*
* @param String base64
* @return byte[]
* @throws IOException
* */
public static byte[] base64ToByteArray(String base64) throws IOException {
if (base64 == null)
return null;
return (new sun.misc.BASE64Decoder()).decodeBuffer(base64);
} /**
* 将base64 字符串反序列化为指定的类
*
* @param base64 序列化后用Base64编码的字符串
* @param serializedClass 序列化对象的class
*
* @return typed object, can be null.
* */
@SuppressWarnings("unchecked")
public static <T> T deFromBase64(String base64, Class<T> serializedClass) {
Object o = null;
byte[] ba = null;
ByteArrayInputStream bai = null;
ObjectInputStream oi = null;
try {
ba = Base64.base64ToByteArray(base64);
bai = new ByteArrayInputStream(ba);
oi = new ObjectInputStream(bai);
o = oi.readObject();
} catch (IOException e) {
System.out.println("反序列化失败!");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("类未找到!");
e.printStackTrace();
} finally {
try {
if (oi != null) {
oi.close();
}
if (bai != null) {
bai.close();
}
} catch (IOException e) {
System.out.println("关闭流异常!");
e.printStackTrace();
}
}
return o == null ? null : (T) o;
} /**
* 将对象序列化为Base64 字符串
*
* @param obj 实现了可序列化接口的对象
* @return String 对象序列化为字符串后编码为Base64
* */
public static String se2base64(Serializable obj) {
byte[] ba = null;
ByteArrayOutputStream baos = null;
ObjectOutputStream oos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
ba = baos.toByteArray();
} catch (IOException e) {
System.out.println("序列化失败!");
} finally {
try {
if (baos != null) {
baos.close();
}
if (oos != null) {
oos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return ba == null ? null : Base64.getBase64(ba);
}
}

测试:

import java.io.Serializable;

public class Test {

    public static void main(String[] args) {
Person person = new Person();
person.setName("Xiaoming");
person.setAge(11);
String s = Base64.se2base64(person);
Person xiaoming = Base64.deFromBase64(s, Person.class);
System.out.println(xiaoming.toString());
} } class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}

运行结果:

Person [name=Xiaoming, age=]

Serializable 序列化为字符串 base64的更多相关文章

  1. Serializable 序列化为文件

    package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...

  2. 使用Newtonsoft进行JSON序列化时将枚举序列化为字符串的方法

    一.实体书写 将枚举类型的属性前面加上[JsonConverter(typeof(StringEnumConverter))]即可. 二.举例 [JsonConverter(typeof(String ...

  3. 将 C# 枚举序列化为 JSON 字符串 基础理论

    该转换过程需要引用 Newtonsoft.JSON,这其中的转换过程还是蛮有意思的. 一.定义枚举 /// <summary> /// 托寄物品枚举 /// </summary> ...

  4. 使用DataContractJsonSerializer类将类型实例序列化为JSON字符串和反序列化为实例对象 分类: JSON 前端 2014-11-10 10:20 97人阅读 评论(1) 收藏

    一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...

  5. @JsonInclude(Include.NON_NULL) resttemplate 传递实体参数时 序列化为json时 空字符串不参与序列化

    @JsonInclude(Include.NON_NULL) resttemplate 传递实体参数时 序列化为json时 空字符串不参与序列化 https://www.cnblogs.com/sup ...

  6. Redis入门 – Jedis存储Java对象 - (Java序列化为byte数组方式)

    Redis入门 – Jedis存储Java对象 - (Java序列化为byte数组方式) 原文地址:http://alanland.iteye.com/admin/blogs/1600685(欢迎转载 ...

  7. Jquery 将表单序列化为Json对象

    大家知道Jquery中有serialize方法,可以将表单序列化为一个“&”连接的字符串,但却没有提供序列化为Json的方法.不过,我们可以写一个插件实现. 我在网上看到有人用替换的方法,先用 ...

  8. jQuery表单验证以及将表单序列化为json对象小练习

    jquery表单验证(非实时验证),同时,将表单序列化为json对象提交表单. <!DOCTYPE html> <html lang="en"> <h ...

  9. JavaScriptSerializer类 对象序列化为JSON,JSON反序列化为对象

    JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.说白了就是能够直接将一个C#对象传送到前台页面成为javascript对 ...

随机推荐

  1. 1124. Mosaic(dfs)

    1124 需要想那么一点点吧 一个连通块中肯定不需要伸进手不拿的情况 不是一个肯定会需要这种情况 然后注意一点 sum=0的时候 就输出0就可以了 不要再减一了 #include <iostre ...

  2. Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)

    题目 题意:求任意连续序列的最大值,这个连续序列可以和其他的 值交换k次,求最大值 思路:暴力枚举所有的连续序列.没做对是因为 首先没有认真读题,没看清交换,然后,以为是dp或者贪心 用了一下贪心,各 ...

  3. uva10375 Choose and divide

    唯一分解定理. 挨个记录下每个质数的指数. #include<cstdio> #include<algorithm> #include<cstring> #incl ...

  4. ffmpeg开发指南

    FFmpeg是一个集录制.转换.音/视频编码解码功能为一体的完整的开源解决方案.FFmpeg的开发是基于Linux操作系统,但是可以在大多数操作系统中编译和使用.FFmpeg支持MPEG.DivX.M ...

  5. ffmpeg windows 雪花声解决方法

    替换所有文件里的<math.h>为<mathimf.h>即可. 我用ffmpeg-0.6.3版测试时,好像mathimf.h文件和其他文件有冲突,需要修改源码. 和qdm2.c ...

  6. 【RMQ问题】求数组区间最大值,NYOJ-1185-最大最小值

    转自:http://blog.csdn.net/lilongherolilong/article/details/6624390 先挖好坑,明天该去郑轻找虐 RMQ(Range Minimum/Max ...

  7. @section Right

    布局页 _BaseLayout.cshtml @RenderSection("Right", true) 视图页 Index.cshtml @{ Layout = "~/ ...

  8. asp调用.net xml web services

    来源:http://www.cnblogs.com/notus/archive/2006/08/10/473000.html#2662503 (是不是实际上可以用这个办法调用任何xml web ser ...

  9. HDU-4035 Maze

    http://acm.hdu.edu.cn/showproblem.php?pid=4035 树上的概率dp.   Maze Time Limit: 2000/1000 MS (Java/Others ...

  10. redo文件四

    v$session_wait 用来查询redo buffer的空间信息 select sid,event,seconds_in_wait,state from v$session_wait where ...