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);
}
}

Base64.java 工具类的更多相关文章

  1. java工具类系列 (四.SerializationUtils)

    java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...

  2. Java工具类——通过配置XML验证Map

    Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...

  3. 排名前 16 的 Java 工具类

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  4. 排名前16的Java工具类

    原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...

  5. 第一章 Java工具类目录

    在这一系列博客中,主要是记录在实际开发中会常用的一些Java工具类,方便后续开发中使用. 以下的目录会随着后边具体工具类的添加而改变. 浮点数精确计算 第二章 Java浮点数精确计算 crc32将任意 ...

  6. java工具类之按对象中某属性排序

    import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...

  7. 干货:排名前16的Java工具类

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  8. Java工具类:给程序增加版权信息

       我们九天鸟的p2p网贷系统,基本算是开发完成了.   现在,想给后端的Java代码,增加版权信息.   手动去copy-paste,太没有技术含量. 于是,写了个Java工具类,给Java源文件 ...

  9. 常用高效 Java 工具类总结

    一.前言 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码 ...

随机推荐

  1. rapidxml使用

    以前都是用tinyxml,这次开发中解析xml配置文件像尝试一下rapidxml,据说效率很高... RapidXml Manual: http://rapidxml.sourceforge.net/ ...

  2. poj 2115 C Looooops(扩展gcd)

    题目链接 这个题犯了两个小错误,感觉没错,结果怒交了20+遍,各种改看别人题解,感觉思路没有错误,就是wa. 后来看diccuss和自己查错,发现自己的ecgcd里的x*(a/b)写成了x*a/b.还 ...

  3. sscanf() 和 sprintf()的用法。

    因为感觉比较有用. 这几次比赛,用过几次,所以写个程序,总结一下. 如果用sscanf(s, "%d.%d", &a, &b); 的时候,一定要注意是否s里一定有小 ...

  4. Android布局详解之一:FrameLayout

      原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...

  5. btr_pcur_t

    /** Persistent cursor */ typedef struct btr_pcur_struct btr_pcur_t; /* The persistent B-tree cursor ...

  6. ha_innobase::rnd_next

    /*****************************************************************//** Reads the next row in a table ...

  7. linux的HugePage与oracle amm关系

     如果Oracle 是11g以后的版本,那么默认创建的实例会使用Automatic Memory Management (AMM)的特性,该特性与HugePage不兼容. 在设置HugePage之前需 ...

  8. codevs 3123 高精度练习之超大整数乘法

    fft. #include<iostream> #include<cstdio> #include<cstring> #include<complex> ...

  9. C++实现String

    # include <iostream> # include <memory> # include <cstring> using namespace std; c ...

  10. MVC&WebForm对照学习:传值方式

    刚从webform开发转到mvc,如果说像路由这样稍微复杂一点的知识点还可以暂时先放一放(前提是默认的路由规则基本满足大部分需求),那有个问题在快速开发中,我想是必须要当即解决的,那就是webform ...