首页

基于apache的commons-codec源码包Base64实现的Base64Utils编码解码工具包代码示例说明

标签:apache,commons-codec,Base64,Base64Utils,编码解码工具包     发布时间:2018-06-24   

一、前言

基于apachecommons-codec源码包中org.apache.commons.codec.binary.Base64加解密编码解码工具包类,实现了BASE64字符串解码decode为二进制数据、二进制数据编码encode为BASE64字符串、将文件编码为BASE64字符串encodeFile、BASE64字符串转回文件decodeToFile、文件转换为二进制数组fileToByte、二进制数据写文件byteArrayToFile等方法,详情示例代码部分。

二、代码示例

import java.io.ByteArrayInputStream;  @b@import java.io.ByteArrayOutputStream;  @b@import java.io.File;  @b@import java.io.FileInputStream;  @b@import java.io.FileOutputStream;  @b@import java.io.InputStream;  @b@import java.io.OutputStream;  @b@import org.apache.commons.codec.binary.Base64;@b@  @b@/** @b@ * <p> @b@ * BASE64编码解码工具包 @b@ * </p> @b@ * <p> @b@ * 依赖javabase64-1.3.1.jar @b@ * </p>  @b@ */  @b@public class Base64Utils {  @b@  @b@    /**  @b@     * 文件读取缓冲区大小 @b@     */  @b@    private static final int CACHE_SIZE = 1024;  @b@      @b@    /** @b@     * <p> @b@     * BASE64字符串解码为二进制数据 @b@     * </p> @b@     *  @b@     * @param base64 @b@     * @return @b@     * @throws Exception @b@     */  @b@    public static byte[] decode(String base64) throws Exception {  @b@        return Base64.decodeBase64(base64.getBytes());  @b@    }  @b@      @b@    /** @b@     * <p> @b@     * 二进制数据编码为BASE64字符串 @b@     * </p> @b@     *  @b@     * @param bytes @b@     * @return @b@     * @throws Exception @b@     */  @b@    public static String encode(byte[] bytes) throws Exception {  @b@        return new String(Base64.encodeBase64(bytes));  @b@    }  @b@      @b@    /**  @b@     * <p> @b@     * 将文件编码为BASE64字符串 @b@     * </p> @b@     * <p> @b@     * 大文件慎用,可能会导致内存溢出 @b@     * </p> @b@     *  @b@     * @param filePath 文件绝对路径 @b@     * @return @b@     * @throws Exception @b@     */  @b@    public static String encodeFile(String filePath) throws Exception {  @b@        byte[] bytes = fileToByte(filePath);  @b@        return encode(bytes);  @b@    }  @b@      @b@    /**  @b@     * <p> @b@     * BASE64字符串转回文件 @b@     * </p> @b@     *  @b@     * @param filePath 文件绝对路径 @b@     * @param base64 编码字符串 @b@     * @throws Exception @b@     */  @b@    public static void decodeToFile(String filePath, String base64) throws Exception {  @b@        byte[] bytes = decode(base64);  @b@        byteArrayToFile(bytes, filePath);  @b@    }  @b@      @b@    /**  @b@     * <p> @b@     * 文件转换为二进制数组 @b@     * </p> @b@     *  @b@     * @param filePath 文件路径 @b@     * @return @b@     * @throws Exception @b@     */  @b@    public static byte[] fileToByte(String filePath) throws Exception {  @b@        byte[] data = new byte[0];  @b@        File file = new File(filePath);  @b@        if (file.exists()) {  @b@            FileInputStream in = new FileInputStream(file);  @b@            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);  @b@            byte[] cache = new byte[CACHE_SIZE];  @b@            int nRead = 0;  @b@            while ((nRead = in.read(cache)) != -1) {  @b@                out.write(cache, 0, nRead);  @b@                out.flush();  @b@            }  @b@            out.close();  @b@            in.close();  @b@            data = out.toByteArray();  @b@         }  @b@        return data;  @b@    }  @b@      @b@    /**  @b@     * <p> @b@     * 二进制数据写文件 @b@     * </p> @b@     *  @b@     * @param bytes 二进制数据 @b@     * @param filePath 文件生成目录 @b@     */  @b@    public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {  @b@        InputStream in = new ByteArrayInputStream(bytes);     @b@        File destFile = new File(filePath);  @b@        if (!destFile.getParentFile().exists()) {  @b@            destFile.getParentFile().mkdirs();  @b@        }  @b@        destFile.createNewFile();  @b@        OutputStream out = new FileOutputStream(destFile);  @b@        byte[] cache = new byte[CACHE_SIZE];  @b@        int nRead = 0;  @b@        while ((nRead = in.read(cache)) != -1) {     @b@            out.write(cache, 0, nRead);  @b@            out.flush();  @b@        }  @b@        out.close();  @b@        in.close();  @b@    }  @b@      @b@      @b@}