如何检索Google文档中托管的不受支持的文档的下载链接?

时间:2012-07-18 14:58:38

标签: java gdata google-docs-api

Google的官方文档告诉我们:

 The download URL for files looks something like this:
 https://doc-04-20-docs.googleusercontent.com/docs/secure/m7an0emtau/WJm12345/YzI2Y2ExYWVm?h=16655626&e=download&gd=true

它还告诉我们文档条目xml如下所示:

<entry ...
...
<content type='application/zip' src='https://doc-0s-84-docs.googleusercontent.com/docs/securesc/4t...626&amp;e=download&amp;gd=true'/>
...
</entry>

但无论我尝试使用gdata java客户端库,我都无法检索该网址。我尝试了所有.get * Link *()方法和.getContent()。有人遇到了这个问题并找到了解决方案吗?我还尝试获取mediasource并处理其输入流。

最终的结果是在我的Java应用服务器(GAE)上获取文件的内容(文件是带有自定义格式的二进制文件),然后将其发送给我的客户端,客户端可以解析它并查看它。

干杯,

Ricola3D

1 个答案:

答案 0 :(得分:0)

最后我自己找到了解决方案,似乎我只是错误地使用它。如果有一天其他人需要,这是代码!

URL entryUrl = new URL("https://docs.google.com/feeds/default/private/full/"+mydocumentid);
DocumentEntry mydocument = client.getEntry(entryUrl, DocumentEntry.class);
if (mydocument!=null) { // if we can read the document
  MediaContent content = (MediaContent) mydocument.getContent();
  //URL exportUrl = new URL(content.getUri()); // download url
  MediaSource source = client.getMedia(content);
  InputStream inStream = null;
  OutputStream outStream = null;
  try {
    inStream = source.getInputStream();
    outStream = resp.getOutputStream();
    int c;
    while ((c = inStream.read()) != -1) {
      outStream.write(c); // copy the stream, by 1byte per 1byte is very slow !
    }
  } finally {
    if (inStream != null) {
      inStream.close();
    }
    if (outStream != null) {
      outStream.flush();
      outStream.close();
    }
  }