android:解析大部分数据
我正在玩 android 并尝试接收文件(pdf 书)。为此,我编写了 Servelt,将书编码为 Base64 字符串并将其放入 XML 文档中。该文档还包含姓名、作者和 IBSN 字段。我成功收到了。如果是一本小书,我什至可以解码并打开它。但如果大小超过 2mb,我会收到 OutOfMeoryError。 我的解析器代码是:
public void characters(char[] ch, int start, int length)
throws SAXException {
if(builder==null) builder = new StringBuilder();
builder.append(new String(ch, start, length));
然后我执行以下操作:
fos = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/book.pdf", false);
byte[] toWrite = Base64.decode(builder.toString(), Base64.DEFAULT);
fos.write(toWrite);
fos.flush();
fos.close();
有谁知道如何解析它而不会出现错误?我试图解析它 字符方法(我的意思是使用小缓冲区)但它失败了...非法的 base 64 字符串。
I'm playing with android and trying to receive file (pdf book). To do this I wrote Servelt that encode the book into base64 string and put it in the XML document. That document contains name, author and IBSN fields also. I successfully receve it. In case of a small book I can even decode it and open. But if the size is more than 2mb I get OutOfMeoryError.
My parser code is:
public void characters(char[] ch, int start, int length)
throws SAXException {
if(builder==null) builder = new StringBuilder();
builder.append(new String(ch, start, length));
Then I do the following:
fos = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/book.pdf", false);
byte[] toWrite = Base64.decode(builder.toString(), Base64.DEFAULT);
fos.write(toWrite);
fos.flush();
fos.close();
Does anybody know how can I parse it wothout error? I've tried to parse it in
characters method (I mean using small buffer) but it fails... Illegal base 64 string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能不能,因为没有足够的 RAM 可供您使用。
明智的程序员不会将大型二进制文件转换为“base64 字符串并将其放入 XML 文档中”。明智的程序员会:
除了这三个之外,可能还有其他解决方案,但它们应该为您提供一个起点。
You can't, most likely, as there is not enough RAM for you to work with.
Sensible programmers would not convert a large binary file into "base64 string and put it in the XML document". Sensible programmers would:
There may be other solutions than those three, but they should give you a starting point.