|
| 1 | +###图片转Base64并压缩 |
| 2 | + |
| 3 | +首先需要Apache下的两个jar包 |
| 4 | + |
| 5 | + <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> |
| 6 | + <dependency> |
| 7 | + <groupId>commons-codec</groupId> |
| 8 | + <artifactId>commons-codec</artifactId> |
| 9 | + <version>1.10</version> |
| 10 | + </dependency> |
| 11 | + <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> |
| 12 | + <dependency> |
| 13 | + <groupId>commons-io</groupId> |
| 14 | + <artifactId>commons-io</artifactId> |
| 15 | + <version>2.4</version> |
| 16 | + </dependency> |
| 17 | +```Java |
| 18 | + package com.silene.base64; |
| 19 | + |
| 20 | + import java.io.ByteArrayInputStream; |
| 21 | + import java.io.ByteArrayOutputStream; |
| 22 | + import java.io.File; |
| 23 | + import java.io.FileInputStream; |
| 24 | + import java.io.IOException; |
| 25 | + import java.util.zip.GZIPInputStream; |
| 26 | + import java.util.zip.GZIPOutputStream; |
| 27 | + |
| 28 | + import org.apache.commons.codec.binary.Base64; |
| 29 | + import org.apache.commons.io.FileUtils; |
| 30 | + |
| 31 | + public class TestBase64Zip { |
| 32 | + |
| 33 | + public static void main(String[] args) { |
| 34 | + |
| 35 | + String base64 = base64("e:\\question.jpg"); |
| 36 | + System.out.println("经过base64转码和压缩" + base64); |
| 37 | + |
| 38 | + decode(base64, "test.jpg", "e:\\"); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * 把经过压缩过的base64串解码解压并写入打磁盘中 |
| 43 | + * @param base64 压缩过的base64串 |
| 44 | + * @param fileName 文件名 |
| 45 | + * @param path 路径地址 |
| 46 | + */ |
| 47 | + public static void decode(String base64, String fileName, String path) { |
| 48 | + //解码 |
| 49 | + byte[] data = Base64.decodeBase64(base64); |
| 50 | + data = unGZip(data); |
| 51 | + writeFile(data, fileName, path); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 二进制文件写入文件 |
| 56 | + * @param data 二进制数据 |
| 57 | + * @param fileName 文件名 |
| 58 | + * @param path 路径地址 |
| 59 | + */ |
| 60 | + public static void writeFile(byte[] data, String fileName, String path) { |
| 61 | + try |
| 62 | + { |
| 63 | + String url = path + "//" + fileName; |
| 64 | + FileUtils.writeByteArrayToFile(new File(url), data); |
| 65 | + } |
| 66 | + catch (IOException e) |
| 67 | + { |
| 68 | + System.out.println("写文件出错" + e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 解壓Gzip |
| 74 | + * @param data |
| 75 | + * @return |
| 76 | + */ |
| 77 | + public static byte[] unGZip(byte[] data){ |
| 78 | + byte[] b = null; |
| 79 | + try{ |
| 80 | + ByteArrayInputStream bis = new ByteArrayInputStream(data); |
| 81 | + GZIPInputStream gzip = new GZIPInputStream(bis); |
| 82 | + byte[] buf = new byte[1024]; |
| 83 | + int num = -1; |
| 84 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 85 | + while ((num = gzip.read(buf, 0, buf.length)) != -1) |
| 86 | + { |
| 87 | + baos.write(buf, 0, num); |
| 88 | + } |
| 89 | + b = baos.toByteArray(); |
| 90 | + baos.flush(); |
| 91 | + baos.close(); |
| 92 | + gzip.close(); |
| 93 | + bis.close(); |
| 94 | + } |
| 95 | + catch (Exception ex){ |
| 96 | + System.out.println("解压数据流出错!!" + ex); |
| 97 | + } |
| 98 | + return b; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * 读取文件并压缩数据然后转Base64编码 |
| 103 | + * @param pathName 图片的绝对路径地址 |
| 104 | + * @return |
| 105 | + */ |
| 106 | + public static String base64(String pathName) { |
| 107 | + byte[] data = getPicData(pathName); |
| 108 | + if (data == null) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + byte[] zipData = gZip(data); |
| 112 | + return Base64.encodeBase64String(zipData); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @description 获取图片的二进制数据 |
| 117 | + * @param pathName 图片的绝对路径地址 |
| 118 | + * @return |
| 119 | + */ |
| 120 | + public static byte[] getPicData(String pathName) { |
| 121 | + byte[] data = null; |
| 122 | + try { |
| 123 | + FileInputStream fi = new FileInputStream(pathName); |
| 124 | + int length = fi.available(); |
| 125 | + data = new byte[length]; |
| 126 | + fi.read(data); |
| 127 | + fi.close(); |
| 128 | + } catch (Exception e) { |
| 129 | + System.out.println(e); |
| 130 | + } |
| 131 | + return data; |
| 132 | + } |
| 133 | + |
| 134 | + /*** |
| 135 | + * @description 压缩GZip |
| 136 | + * @param data 要压缩的二进制数据 |
| 137 | + * @return |
| 138 | + */ |
| 139 | + public static byte[] gZip(byte[] data) { |
| 140 | + byte[] b = null; |
| 141 | + try { |
| 142 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 143 | + GZIPOutputStream gzip = new GZIPOutputStream(bos); |
| 144 | + gzip.write(data); |
| 145 | + gzip.finish(); |
| 146 | + gzip.close(); |
| 147 | + b = bos.toByteArray(); |
| 148 | + bos.close(); |
| 149 | + } catch (Exception ex) { |
| 150 | + ex.printStackTrace(); |
| 151 | + } |
| 152 | + return b; |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | +``` |
0 commit comments