在做项目的时候遇到需要将文件转为base64编码,并存储在文件中。

在将文件转为base64编码是会将文件读入内存,进行base64编码,输出到文件中。代码入下:

   FileInputStream stream = new FileInputStream("D:\\桌面\\程序员-第4版.pdf");
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] b = new byte[1024];
int n;
while ((n = stream.read(b)) != -1) {
out.write(b, 0, n);
}
stream.close();
out.close();
System.out.println(new String(Base64.encodeBase64(out.toByteArray())));

  但是大文件在进行base64编码的时候就会遇到OOM(OOM为out of memory的简称,称之为内存溢出)。

  产生OOM的原因:

  • 文件太大,超出了内存
  • 文件可以正常读入内存,由于base64编码后的文件比原来的文件大1/3,在编码的过程中超出内存

由于3个常规字符可以转换为4个base64编码字符,所以使用3的公倍数作为缓冲区大小。

所以在对大文件进行base64编码时可以采用分段编码,进行输出。代码入下:

//使用分段上传的读取文件的方式将大文件转换为base64编码数据
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream("D:\\桌面\\程序员-第4版.pdf");
byte[] byteBuf = new byte[3 * 1024 * 1024];
byte[] base64ByteBuf;
int count1; //每次从文件中读取到的有效字节数
while ((count1 = file1.read(byteBuf)) != -1) {
if (count1 != byteBuf.length) {//如果有效字节数不为3*1000,则说明文件已经读到尾了,不够填充满byteBuf了
byte[] copy = Arrays.copyOf(byteBuf, count1); //从byteBuf中截取包含有效字节数的字节段
base64ByteBuf = Base64.encodeBase64(copy); //对有效字节段进行编码
} else {
base64ByteBuf = Base64.encodeBase64(byteBuf);
}
os1.write(base64ByteBuf, 0, base64ByteBuf.length);
os1.flush();
}
file1.close();
System.out.println(os1.toString());

  以上代码是将编码后的数据输出至控制台。其实最好是将文件分段进行编码,分段输出,这样不管文件多大,都可以进行编码,并且不会OOM。以下是将文件输出至txt文档中:

 ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream("D:\\桌面\\程序员-第4版.pdf");
byte[] byteBuf = new byte[3 * 1024 * 1024];
byte[] base64ByteBuf;
int count1; //每次从文件中读取到的有效字节数
File file = new File("D:\\1.txt");
while ((count1 = file1.read(byteBuf)) != -1) {
if (count1 != byteBuf.length) {//如果有效字节数不为3*1000,则说明文件已经读到尾了,不够填充满byteBuf了
byte[] copy = Arrays.copyOf(byteBuf, count1); //从byteBuf中截取包含有效字节数的字节段
base64ByteBuf = Base64.encodeBase64(copy); //对有效字节段进行编码
} else {
base64ByteBuf = Base64.encodeBase64(byteBuf);
}
FileUtils.writeByteArrayToFile(file, base64ByteBuf, true); // 将转换后的数据写入文件中,该方法会自动创建文件
os1.flush();
}
file1.close();

  本文参考文档:https://blog.csdn.net/u014248939/article/details/53205030/

base64解码大文件请参考:https://blog.csdn.net/GAMEloft9/article/details/88536661,没有实际操作,原理上应该是可以的。

  

最新文章

  1. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
  2. 测试开发面试-java持续累积
  3. sql中查询中的when...then 语句
  4. BZOJ 1251: 序列终结者
  5. Chorme中启动阿里旺旺误点取消启动并记住选择,如何更改。
  6. Linux下监控磁盘使用量并在超过阀值后自动发送报警邮件
  7. C++之路进阶——codevs2313(星际竞速)
  8. Ueditor图片缩放的设置
  9. ORA-00001: unique constraint (...) violated解决方案
  10. Java设计模式之工厂模式(简单工厂模式+工厂方法模式)
  11. Debug目录下没有.exe文件
  12. Javascript做模糊查询
  13. 对于Hadoop的MapReduce编程makefile
  14. hdu2063 匈牙利算法 二分最大匹配模版题
  15. Convolutional LSTM Network: A Machine LearningApproach for Precipitation Nowcasting
  16. Java高级应用简笔
  17. Zk 集群概念
  18. Wincc用户登录VBS脚本
  19. typedef 详解
  20. python + lisp hy的新手注记1

热门文章

  1. 深度解析 Qt 中动态链接库
  2. Could not attach to a Hearthstone process.
  3. C# 7 .NET / CLR / Visual Studio version requirements
  4. linux系统空间不足,不重启进程,清理僵尸文件。
  5. qt5之网络通信
  6. 微服务中的CAP定律
  7. input 表单
  8. 让 Nginx 支持 WAF 防护功能实战
  9. C++ STL内存池
  10. Topic与Partition