支援續傳的媒體下載功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
從伺服器下載大型媒體檔案時,請使用
續傳媒體下載,以分段下載檔案。Google API
產生的程式庫包含方便與支援續傳互動的便利方法
媒體下載
支援續傳的媒體下載通訊協定與支援續傳的媒體上傳作業相似
相關資訊,詳見
Google Drive API 說明文件。
實作詳情
您感興趣的主要類別為 MediaHttpDownloader 和 MediaHttpDownloaderProgressListener。
媒體內容會以分塊的形式下載,也可以設定區塊大小。如果
要求期間發生伺服器錯誤,然後重試此要求。
如果服務專屬產生程式庫中的方法支援下載
探索文件,然後
方便的下載方法
OutputStream。
(如要進一步瞭解透過「Google API 探索服務」使用媒體下載的資訊,請參閱
媒體下載)。
例如:
class CustomProgressListener implements MediaHttpDownloaderProgressListener {
public void progressChanged(MediaHttpDownloader downloader) {
switch (downloader.getDownloadState()) {
case MEDIA_IN_PROGRESS:
System.out.println(downloader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Download is complete!");
}
}
}
OutputStream out = new FileOutputStream("/tmp/driveFile.jpg");
DriveFiles.Get request = drive.files().get(fileId);
request.getMediaHttpDownloader().setProgressListener(new CustomProgressListener());
request.executeMediaAndDownloadTo(out);
即使沒有服務專用的程式庫,也能使用這項功能。
範例如下:
OutputStream out = new FileOutputStream("/tmp/Test.jpg");
MediaHttpDownloader downloader = new MediaHttpDownloader(transport, httpRequestInitializer);
downloader.setProgressListener(new CustomProgressListener());
downloader.download(requestUrl, out);
支援續傳的媒體下載功能預設為啟用,但您可以將其停用,然後改用
您可直接下載媒體,例如下載小型檔案時。
我們在
1.9.0-beta
適用於 Java 的 Google API 用戶端程式庫版本。
直接媒體下載作業會透過一個 HTTP 要求,下載整個媒體內容,
與支援續傳的媒體下載通訊協定
這種通訊協定可透過多種方式
要求。直接下載會減少 HTTP 要求數量,但
會增加可能發生失敗 (例如連線失敗) 的機率
下載大量資料
上文的用法與上述說明相同,但系統會新增以下項目:
呼叫
MediaHttpDownloader
直接下載:
mediaHttpDownloader.setDirectDownloadEnabled(true);
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eDownload large files efficiently with resumable media download, splitting the process into smaller chunks.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the \u003ccode\u003eMediaHttpDownloader\u003c/code\u003e and \u003ccode\u003eMediaHttpDownloaderProgressListener\u003c/code\u003e classes for managing and monitoring downloads.\u003c/p\u003e\n"],["\u003cp\u003eCustomize the download process by implementing a progress listener to track download state and progress.\u003c/p\u003e\n"],["\u003cp\u003eOpt for direct media download for smaller files, combining the download into a single HTTP request.\u003c/p\u003e\n"],["\u003cp\u003eResumable media download is enabled by default, but direct download can be activated using \u003ccode\u003emediaHttpDownloader.setDirectDownloadEnabled(true)\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Resumable Media Downloads\n\nWhen you download a large media file from a server, use\n*resumable media download* to download the file chunk by chunk. The Google API\ngenerated libraries contain convenience methods for interacting with resumable\nmedia download.\n\nThe resumable media download protocol is similar to the resumable media upload\nprotocol, which is described in the\n[Google Drive API documentation](https://developers.google.com/drive/web/manage-uploads#resumable).\n\nImplementation details\n----------------------\n\nThe main classes of interest are [MediaHttpDownloader](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/media/MediaHttpDownloader.html) and [MediaHttpDownloaderProgressListener](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/media/MediaHttpDownloaderProgressListener.html).\nMedia content is downloaded in chunks, and chunk size is configurable. If a\nserver error is encountered in a request, then the request is retried.\n\nIf methods in the service-specific generated libraries support download in the\n[Discovery document](https://developers.google.com/discovery/v1/using), then a\nconvenient download method is created for these methods that takes in an\n[OutputStream](http://docs.oracle.com/javase/1.5.0/docs/api/org/omg/CORBA/portable/OutputStream.html).\n(For more about using media download with the Google APIs Discovery Service, see\n[Media download](https://developers.google.com/discovery/v1/using#discovery-doc-methods-mediadownload).)\n\nFor example: \n\n class CustomProgressListener implements MediaHttpDownloaderProgressListener {\n public void progressChanged(MediaHttpDownloader downloader) {\n switch (downloader.getDownloadState()) {\n case MEDIA_IN_PROGRESS:\n System.out.println(downloader.getProgress());\n break;\n case MEDIA_COMPLETE:\n System.out.println(\"Download is complete!\");\n }\n }\n }\n\n OutputStream out = new FileOutputStream(\"/tmp/driveFile.jpg\");\n\n DriveFiles.Get request = drive.files().get(fileId);\n request.getMediaHttpDownloader().setProgressListener(new CustomProgressListener());\n request.executeMediaAndDownloadTo(out);\n\nYou can also use this feature without service-specific generated libraries.\nHere is an example: \n\n OutputStream out = new FileOutputStream(\"/tmp/Test.jpg\");\n\n MediaHttpDownloader downloader = new MediaHttpDownloader(transport, httpRequestInitializer);\n downloader.setProgressListener(new CustomProgressListener());\n downloader.download(requestUrl, out);\n\nDirect media download\n---------------------\n\nResumable media download is enabled by default, but you can disable it and use\ndirect media download instead, for example if you are downloading a small file.\nDirect media download was introduced in the\n[1.9.0-beta](http://google-api-java-client.blogspot.com/2012/05/version-190-beta-released.html)\nversion of the Google API Client Library for Java.\n\nDirect media download downloads the whole media content in one HTTP request, as\nopposed to the resumable media download protocol, which can download in multiple\nrequests. Doing a direct download reduces the number of HTTP requests but\nincreases the chance of failures (such as connection failures) that can happen\nwith large downloads.\n\nThe usage is the same as what is described above, plus the following\ncall that tells\n[MediaHttpDownloader](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/media/MediaHttpDownloader.html)\nto do direct downloads: \n\n mediaHttpDownloader.setDirectDownloadEnabled(true);"]]