一)Base64简介
Base64是网络上一种常用于传输字节码的编码方式之一,比较简单通用。
二)Base64文件类型
文件类型:
文件类型 | Base64类型 |
doc | data:application/msword;base64, |
docx | data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64, |
xls | data:application/vnd.ms-excel;base64, |
xlsx | data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64, |
data:application/pdf;base64, | |
ppt | data:application/vnd.ms-powerpoint;base64, |
pptx | data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64, |
txt | data:text/plain;base64, |
图片类型:
图片类型 | Base64类型 |
png | data:image/png;base64, |
jpg | data:image/jpeg;base64, |
jpeg | data:image/jpeg;base64, |
gif | data:image/gif;base64, |
svg | data:image/svg+xml;base64, |
ico | data:image/x-icon;base64, |
bmp | data:image/bmp;base64, |
通用类型:
二进制类型 | Base64类型 |
octet-stream | data:application/octet-stream;base64, |
三)Base64对文件编码、解码
第一步:创建一个maven项目,引入了一个Apache的Base64 jar
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
第二步:创建一个Base64类型枚举类
package com.oysept.base64.enums;
/**
* base64文件类型,前缀
* @author ouyangjun
*/
public enum Base64FileTypeEnum {
// 文件类型
BASE64_FILETYPE_DOC("doc", "data:application/msword;base64,"),
BASE64_FILETYPE_DOCX("docx", "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,"),
BASE64_FILETYPE_XLS("xls", "data:application/vnd.ms-excel;base64,"),
BASE64_FILETYPE_XLSX("xlsx", "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,"),
BASE64_FILETYPE_PDF("pdf", "data:application/pdf;base64,"),
BASE64_FILETYPE_PPT("ppt", "data:application/vnd.ms-powerpoint;base64,"),
BASE64_FILETYPE_PPTX("pptx", "data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,"),
BASE64_FILETYPE_TXT("txt", "data:text/plain;base64,"),
// 图片类型
BASE64_FILETYPE_PNG("png", "data:image/png;base64,"),
BASE64_FILETYPE_JPG("jpg", "data:image/jpeg;base64,"),
BASE64_FILETYPE_JPEG("jpeg", "data:image/jpeg;base64,"),
BASE64_FILETYPE_GIF("gif", "data:image/gif;base64,"),
BASE64_FILETYPE_SVG("svg", "data:image/svg+xml;base64,"),
BASE64_FILETYPE_ICO("ico", "data:image/x-icon;base64,"),
BASE64_FILETYPE_BMP("bmp", "data:image/bmp;base64,"),
// 二进制流
BASE64_FILETYPE_OCTET_STREAM("octet-stream", "data:application/octet-stream;base64,"),
;
private Base64FileTypeEnum(String code, String value) {
this.code = code;
this.value = value;
}
private String code;
private String value;
public String getCode() {return code;}
public String getValue() {return value;}
public static String value(String code) {
Base64FileTypeEnum[] types = values();
for (Base64FileTypeEnum x : types) {
if (x.getCode().equals(code)) {
return x.getValue();
}
}
return null;
}
}
第三步:创建一个Base64对文件编码、解码的工具类
package com.oysept.base64.utils;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.codec.binary.Base64;
import com.oysept.base64.enums.Base64FileTypeEnum;
public class FileUtils {
// 获取文件转换之后的base64内容
public static String encodeBase64File(String prefix, File file) {
try {
if (file == null || !file.exists() || prefix == null) {
return null;
}
long beginTime = System.currentTimeMillis();
// base64文件前缀
String base64Format = Base64FileTypeEnum.value(prefix.toLowerCase());
if (base64Format == null || "".equals(base64Format)) {
return null;
}
// 获取文件流
InputStream in = new FileInputStream(file);
BufferedInputStream bufInput = new BufferedInputStream(in); // 缓存流
// 先把二进制流写入到ByteArrayOutputStream中
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
byte[] bt = new byte[4096];
int len;
while ((len = bufInput.read(bt)) != -1) {
byteArray.write(bt, 0, len);
}
byteArray.flush();
long endTime = System.currentTimeMillis();
System.out.println("==>encodeBase64File, 把文件转换成base64编码, 总耗时: " + (endTime - beginTime) + "ms");
// 返回
return base64Format + Base64.encodeBase64String(byteArray.toByteArray());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// 把base64文件解码
public static String decodeBase64String(String prefix, String base64String) {
try {
if (prefix == null || base64String == null) {
return null;
}
long beginTime = System.currentTimeMillis();
// 把base64前缀截取掉
// base64文件前缀
String value = Base64FileTypeEnum.value(prefix.toLowerCase());
if (value == null || "".equals(value)) {
return null;
}
// 替换
String tempBase64String = base64String.replace(value, "");
// 把base64字符串转换成字节
byte[] bytes = Base64.decodeBase64(tempBase64String);
// 转换成字节输入流
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
// 把base64编码文件还原, 并存放到指定磁盘路径中
OutputStream out = new FileOutputStream(new File("D:\\new_test." + prefix));
// 写文件
byte[] buffer = new byte[4096];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len); // 文件写操作
}
long endTime = System.currentTimeMillis();
System.out.println("==>decodeBase64String, 解析base64编码文件, 总耗时: " + (endTime - beginTime) + "ms");
return "success";
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
第四步:用main方法测试
package com.oysept.base64;
import java.io.File;
import com.oysept.base64.utils.FileUtils;
public class LocalBase64Test {
public static void main(String[] args) {
// 获取文件, 对文件base64编码
File file = new File("D:\\test.txt");
String prefix = "txt";
// base64文件内容
String base64String = FileUtils.encodeBase64File(prefix, file);
System.out.println("文件base64加密之后的内容(太长, 控制台可能打印不出来): \n" + base64String);
// 获取base64文件, 进行解码
FileUtils.decodeBase64String(prefix, base64String);
}
}
base64编码前:
base64编码后:
识别二维码关注个人微信公众号
本章完结,待续,欢迎转载!
本文说明:该文章属于原创,如需转载,请标明文章转载来源!