簡體   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