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}
Base64工具类
Stella981
2021-10-11
2056 0 0
点赞
收藏
评论区
加载中...