博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Base64.java 工具类
阅读量:4624 次
发布时间:2019-06-09

本文共 3259 字,大约阅读时间需要 10 分钟。

 

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 deFromBase64(String base64, Class
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); }}

 

转载于:https://www.cnblogs.com/zno2/p/4757614.html

你可能感兴趣的文章
一文搞懂Java环境,轻松实现Hello World!
查看>>
hash实现锚点平滑滚动定位
查看>>
也谈智能手机游戏开发中的分辨率自适应问题
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
查看>>
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>
Hibernate composite key
查看>>
[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
查看>>
keepalived+nginx安装配置
查看>>
vue+element-ui实现表格checkbox单选
查看>>
autofac
查看>>
MacOS 系统终端上传文件到 linux 服务器
查看>>
Excel导出POI
查看>>