使用 google api 读取 google 文本文档时出现问题

发布于 2024-12-21 00:19:56 字数 829 浏览 1 评论 0原文

我正在尝试使用以下代码来读取 Google 文本文档。但返回的值是一个带有垃圾字符的流,而不是真正的内容。我该如何解决这个问题。

for (DocumentListEntry entry : resultFeed.getEntries()) {  
    String docId = entry.getDocId();
    String docType = entry.getType();
    URL exportUrl = new URL("https://docs.google.com/feeds/download/"
        + docType
        + "s/Export?docID="
        + docId
        + "&exportFormat=doc");

    MediaContent mc = new MediaContent();
    mc.setUri(exportUrl.toString());

    MediaSource ms = client.getMedia(mc);
    InputStream inStream = null;


    try {
        inStream = ms.getInputStream();
        int c;
        while ((c = inStream.read()) != -1) {
            System.out.print((char)c);
        }
    } finally {
        if (inStream != null) {
            inStream.close();
        }
    }
}  

I am trying to use following code to read a Google text document. But the value returned is a stream with garbage characters instead of the real contents. How can I fix this.

for (DocumentListEntry entry : resultFeed.getEntries()) {  
    String docId = entry.getDocId();
    String docType = entry.getType();
    URL exportUrl = new URL("https://docs.google.com/feeds/download/"
        + docType
        + "s/Export?docID="
        + docId
        + "&exportFormat=doc");

    MediaContent mc = new MediaContent();
    mc.setUri(exportUrl.toString());

    MediaSource ms = client.getMedia(mc);
    InputStream inStream = null;


    try {
        inStream = ms.getInputStream();
        int c;
        while ((c = inStream.read()) != -1) {
            System.out.print((char)c);
        }
    } finally {
        if (inStream != null) {
            inStream.close();
        }
    }
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

呢古 2024-12-28 00:19:56

快速阅读文档后,您似乎读取 Microsoft Word 编码文档的原始字节。

尝试将 &exportFormat=doc 更改为 htmltxt 并查看输出是否更有意义。

From a quick read of the documentation, it looks like you are reading the raw bytes of a Microsoft Word-encoded document.

Try changing the &exportFormat=doc to html or txt and see if the output makes more sense.

小耗子 2024-12-28 00:19:56

我怀疑您尝试打印的文件有一些其他编码,但您正在以 ASCII 方式逐字节打印它们。我会尝试将整个流读取为字节数组,然后使用其他编码(例如UTF8)将其转换为字符串。

I suspect that the files you are trying to print out have some other encoding but you're printing them byte by byte in ASCII way. I would try to read the whole stream as byte array and then convert it to string using some other encoding (e.g. UTF8).

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