可续传媒体下载
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
从服务器下载大型媒体文件时,请使用
可续传媒体下载:用于逐块下载文件。Google API
生成的库包含与可续传对象进行交互的便捷方法
下载媒体内容。
可续传媒体下载协议类似于可续传媒体上传
协议,这在
Google Drive API 文档。
实现细节
主要的类是 MediaHttpDownloader 和 MediaHttpDownloaderProgressListener。
媒体内容以分块的形式下载,分块大小可配置。如果
请求遇到服务器错误,则会重试该请求。
如果服务专属生成的库中的方法支持下载
发现文档,然后点按
我们针对这些方法创建了一种便捷的下载方法,
OutputStream。
(有关将媒体下载与 Google API Discovery Service 结合使用的详细信息,请参阅
媒体下载。)
例如:
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);
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\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,["When 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\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\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);"]]