Base64工具类

1package com.locator.encryption; 2 3import java.io.ByteArrayInputStream; 4import java.io.ByteArrayOutputStream; 5import java.io.File; 6import java.io.FileInputStream; 7import java.io.FileOutputStream; 8import java.io.InputStream; 9import java.io.OutputStream; 10 11import it.sauronsoftware.base64.Base64; 12 13/** 14 * <p> 15 * BASE64编码解码工具包 16 * </p> 17 * <p> 18 * 依赖javabase64-1.3.1.jar 19 * </p> 20 *  21 * @author IceWee 22 * @date 2012-5-19 23 * @version 1.0 24 */ 25public class Base64Utils { 26 27    /** 28     * 文件读取缓冲区大小 29     */ 30    private static final int CACHE_SIZE = 1024; 31     32    /** 33     * <p> 34     * BASE64字符串解码为二进制数据 35     * </p> 36     *  37     * @param base64 38     * @return 39     * @throws Exception 40     */ 41    public static byte[] decode(String base64) throws Exception { 42        return Base64.decode(base64.getBytes()); 43    } 44     45    /** 46     * <p> 47     * 二进制数据编码为BASE64字符串 48     * </p> 49     *  50     * @param bytes 51     * @return 52     * @throws Exception 53     */ 54    public static String encode(byte[] bytes) throws Exception { 55        return new String(Base64.encode(bytes)); 56    } 57     58    /** 59     * <p> 60     * 将文件编码为BASE64字符串 61     * </p> 62     * <p> 63     * 大文件慎用,可能会导致内存溢出 64     * </p> 65     *  66     * @param filePath 文件绝对路径 67     * @return 68     * @throws Exception 69     */ 70    public static String encodeFile(String filePath) throws Exception { 71        byte[] bytes = fileToByte(filePath); 72        return encode(bytes); 73    } 74     75    /** 76     * <p> 77     * BASE64字符串转回文件 78     * </p> 79     *  80     * @param filePath 文件绝对路径 81     * @param base64 编码字符串 82     * @throws Exception 83     */ 84    public static void decodeToFile(String filePath, String base64) throws Exception { 85        byte[] bytes = decode(base64); 86        byteArrayToFile(bytes, filePath); 87    } 88     89    /** 90     * <p> 91     * 文件转换为二进制数组 92     * </p> 93     *  94     * @param filePath 文件路径 95     * @return 96     * @throws Exception 97     */ 98    public static byte[] fileToByte(String filePath) throws Exception { 99        byte[] data = new byte[0]; 100        File file = new File(filePath); 101        if (file.exists()) { 102            FileInputStream in = new FileInputStream(file); 103            ByteArrayOutputStream out = new ByteArrayOutputStream(2048); 104            byte[] cache = new byte[CACHE_SIZE]; 105            int nRead = 0; 106            while ((nRead = in.read(cache)) != -1) { 107                out.write(cache, 0, nRead); 108                out.flush(); 109            } 110            out.close(); 111            in.close(); 112            data = out.toByteArray(); 113         } 114        return data; 115    } 116     117    /** 118     * <p> 119     * 二进制数据写文件 120     * </p> 121     *  122     * @param bytes 二进制数据 123     * @param filePath 文件生成目录 124     */ 125    public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { 126        InputStream in = new ByteArrayInputStream(bytes);    127        File destFile = new File(filePath); 128        if (!destFile.getParentFile().exists()) { 129            destFile.getParentFile().mkdirs(); 130        } 131        destFile.createNewFile(); 132        OutputStream out = new FileOutputStream(destFile); 133        byte[] cache = new byte[CACHE_SIZE]; 134        int nRead = 0; 135        while ((nRead = in.read(cache)) != -1) {    136            out.write(cache, 0, nRead); 137            out.flush(); 138        } 139        out.close(); 140        in.close(); 141    } 142     143     144}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Base64工具类 - HelloWorld