הורדות מדיה שניתנות לחידוש
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
כדי להוריד קובץ מדיה גדול משרת, צריך להשתמש ב-
הורדת מדיה שניתן להמשיך כדי להוריד את מקטע הקובץ לפי מקטע. ממשק API של Google
שהספריות שנוצרות מכילות שיטות נוחות לאינטראקציה עם תמונות שניתן להמשיך
הורדת מדיה.
הפרוטוקול להורדת מדיה שניתן להמשיך דומה לפרוטוקול של העלאת מדיה שניתן להמשיך
שמתואר
מאמרי העזרה של Google Drive API
פרטי ההטמעה
סוגי העניין העיקריים הם MediaHttpDownloader ו-MediaHttpDownloaderProgressListener.
המערכת מורידה את תוכן המדיה במקטעי נתונים, וניתן להגדיר את הגודל של מקטע הנתונים. אם
אירעה שגיאת שרת בבקשה, ולאחר מכן מתבצע ניסיון חוזר של הבקשה.
אם שיטות בספריות הספציפיות לשירות שנוצרות תומכות בהורדה במסגרת
מסמך Discovery, ואז
ונוצרת שיטת הורדה נוחה לשימוש בשיטות האלה,
OutputStream.
(למידע נוסף על השימוש בהורדת מדיה עם Google APIs 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
של ספריית הלקוח של Google API עבור Java.
הורדה ישירה של מדיה מורידה את כל תוכן המדיה בבקשת HTTP אחת, כמו
בניגוד לפרוטוקול להורדת מדיה שניתן להמשיך, כי היא יכולה להוריד כמה קבצים בבת אחת
בקשות. הורדה ישירה מפחיתה את מספר בקשות ה-HTTP אבל
מגביר את הסיכוי לכשלים (למשל כשלים בחיבור) שיכולים לקרות
עם כמות גדולה של הורדות.
השימוש זהה למתואר למעלה, בתוספת
שאומרת
MediaHttpDownloader
כדי לבצע הורדות ישירות:
mediaHttpDownloader.setDirectDownloadEnabled(true);
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 4.0 ודוגמאות הקוד הן ברישיון Apache 2.0. לפרטים, ניתן לעיין במדיניות האתר Google Developers. Java הוא סימן מסחרי רשום של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-08-31 (שעון UTC).
[[["התוכן קל להבנה","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 (שעון UTC)."],[[["\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);"]]