繁体   English   中英

如何使用 Java SDK 导出 Google Drive 文件

[英]How do I export a Google Drive file using the Java SDK

以下链接页面上列出的解决方案不涵盖此主题: https : //developers.google.com/drive/web/manage-downloads

经过一些尝试和错误后,我得到了这段代码:

String odt = file.getExportLinks().get("application/pdf");
try (FileOutputStream fos = new FileOutputStream("C:/data/test_" + file.getTitle() + ".pdf")) {
    service.files().get(file.getId()).getMediaHttpDownloader().download(new GenericUrl(odt), fos);
} catch (IOException ioe) {
    ioe.printStackTrace();
}

这里有两种选择:

public static InputStream download(Drive service, String url) throws IOException {
    return service.getRequestFactory().buildGetRequest(new GenericUrl(url)).execute().getContent();
}

public static void download(Drive service, String url, OutputStream os) throws IOException {
    HttpRequestFactory rf = service.getRequestFactory();
    new MediaHttpDownloader(rf.getTransport(), rf.getInitializer()).download(new GenericUrl(url), os);
}

但我确实想知道是否有更惯用的方法来获取 MediaHttpDownloader。

PS:我认为应该在Google文档页面中添加正确的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM