工具类
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=11]
时间: 2024-10-30 08:22:10