android:解析大部分数据

发布于 2024-10-15 20:18:56 字数 777 浏览 1 评论 0原文

我正在玩 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随梦而飞# 2024-10-22 20:18:56

有人知道如何解析它而不出错吗?

您很可能不能,因为没有足够的 RAM 可供您使用。

明智的程序员不会将大型二进制文件转换为“base64 字符串并将其放入 XML 文档中”。明智的程序员会:

  • 将元数据(姓名、作者、ISBN)放在 PDF 的属性字段中,这对于 Java 来说是一个很好的解决方案,但对于 Android 来说可能是一个问题,因为获取这些字段的库可能会也可能不会工作(例如,iText);或
  • 分别下载两个文件(其中一个包含元数据的 XML,其中包含 PDF 文件的 URL);或者
  • 将 PDF 和 XML 文件打包到单个存档(例如 ZIP)中并下载该存档

除了这三个之外,可能还有其他解决方案,但它们应该为您提供一个起点。

Does anybody know how can I parse it wothout error?

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:

  • put the metadata (name, author, ISBN) in property fields within the PDF, which is a fine solution for Java, but may be a problem for Android, as the libraries to get at those fields may or may not work (e.g., iText); or
  • download the two files separately (one with the XML of metadata, which contains the URL to the PDF file); or
  • package the PDF and the XML file into a single archive (e.g., ZIP) and download the archive

There may be other solutions than those three, but they should give you a starting point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文