![](/img/trans.png)
[英]How do I copy a file on a Google Drive using v3 of the Google Drive Java API?
[英]How do I export a Google Drive file using the Java SDK
經過一些嘗試和錯誤后,我得到了這段代碼:
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.