栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用流式传输的Android File To Base64有时会丢失2个字节

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用流式传输的Android File To Base64有时会丢失2个字节

这是在使用base64OutputStream的情况下有效的解决方案。我将在代码后解释其中的一些:

这是代码:

FileOutputStream fout = new FileOutputStream(path + ".txt");FileInputStream fin = new FileInputStream(path);System.out.println("File Size:" + path.length());ByteArrayOutputStream os = new ByteArrayOutputStream();base64OutputStream base64out = new base64OutputStream(os,base64.NO_WRAP);byte[] buffer = new byte[3 * 512];int len = 0;while ((len = fin.read(buffer)) >= 0) {    base64out.write(buffer, 0, len);}System.out.println("Enpred Size:" + os.size());base64out.flush();base64out.close();//this did the tricks. Please see explanation.String result = new String(os.toByteArray(), "UTF-8");fout.write(os.toByteArray());fout.flush();fout.close();os.close();fin.close();return result;

实际上,在通过添加flush()使用Dawnkeeper的建议之后,我只移动了一行代码。这些技巧是在处理任何数据之前在base64out.close()中完成的。关闭base64OutputStream时,可能会将base64的结尾填充写入输出。我的想法来自org.apache.myfaces.trinidad.util.base64OutputStream
close()方法。这就是为什么我尝试。

在数据处理之前关闭OutputStream似乎很奇怪,但是在这种情况下,它仅同时关闭base64OutputStream而不同时关闭ByteArrayOutputStream。因此,数据仍然可以从ByteArrayOutputStream中检索。

感谢Dawnkeeper的努力,并希望这个问题可以帮助其他在OutOfMemory Exception中遭受苦难的人。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507908.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号