تحميلات الوسائط المباشرة والقابلة للاستئناف
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يوضح هذا المستند كيفية استخدام تحميلات الوسائط المباشرة والقابلة للاستئناف مع
مكتبة برامج Google API للغة Java.
تحميل وسائط قابل للاستئناف
عند تحميل ملف وسائط كبير إلى خادم، استخدِم تحميل الوسائط القابل للاستئناف من أجل
إرسال مقطع الملف مقطع. تحتوي المكتبات التي تم إنشاؤها بواسطة Google API على
طريقة سهلة للتفاعل مع تحميل وسائط قابل للاستئناف.
يشبه بروتوكول تحميل الوسائط القابل للاستئناف عملية تحميل الوسائط القابلة للاستئناف.
كما هو موضَّح في مستندات واجهة برمجة تطبيقات Google Drive.
تصميم البروتوكول
يوضح الرسم البياني للتسلسل التالي كيفية عمل بروتوكول تحميل الوسائط القابل للاستئناف:

تفاصيل التنفيذ
إن فئات الاهتمام الرئيسية هي
MediaHttpUploader
وMediaHttpProgressListener.
إذا كانت الطرق في المكتبات التي تم إنشاؤها خاصة بالخدمة تحتوي على mediaUpload
في مستند Discovery،
ثم يتم إنشاء طريقة ملائمة لهذه الطرق التي تتطلب
InputStreamContent
كمعلمة. (لمزيد من المعلومات عن استخدام تحميل الوسائط مع Google APIs
خدمة Discovery، راجع
تحميل الوسائط).
على سبيل المثال، الطريقة insert
لـ Drive API
يدعم mediaUpload
، ويمكنك استخدام الرمز التالي لتحميل ملف:
class CustomProgressListener implements MediaHttpUploaderProgressListener {
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
System.out.println("Initiation has started!");
break;
case INITIATION_COMPLETE:
System.out.println("Initiation is complete!");
break;
case MEDIA_IN_PROGRESS:
System.out.println(uploader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Upload is complete!");
}
}
}
File mediaFile = new File("/tmp/driveFile.jpg");
InputStreamContent mediaContent =
new InputStreamContent("image/jpeg",
new BufferedInputStream(new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
Drive.Files.Insert request = drive.files().insert(fileMetadata, mediaContent);
request.getMediaHttpUploader().setProgressListener(new CustomProgressListener());
request.execute();
يمكنك أيضًا استخدام ميزة تحميل الوسائط القابلة للاستئناف بدون
المكتبات التي تم إنشاؤها. يُرجى الاطّلاع على المثال أدناه:
File mediaFile = new File("/tmp/Test.jpg");
InputStreamContent mediaContent =
new InputStreamContent("image/jpeg",
new BufferedInputStream(new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, transport, httpRequestInitializer);
uploader.setProgressListener(new CustomProgressListener());
HttpResponse response = uploader.upload(requestUrl);
if (!response.isSuccessStatusCode()) {
throw GoogleJsonResponseException(jsonFactory, response);
}
تحميل الوسائط مباشرةً
يتم تمكين تحميل الوسائط القابلة للاستئناف بشكل افتراضي، ولكن يمكنك تعطيله واستخدام
تحميل وسائط مباشرة بدلاً من ذلك، إذا كنت تحمّل ملفًا صغيرًا مثلاً. بشكل مباشر
تم تقديم تحميل الوسائط في الإصدار 1.9.0-beta
من مكتبة برامج Google API للغة Java.
أما التحميل المباشر للوسائط، فهو يحمّل الملف بأكمله في طلب HTTP واحد، بدلاً من
بروتوكول تحميل الوسائط القابل للاستئناف، والذي يحمّل الملف في طلبات متعددة.
يؤدي إجراء تحميل مباشر إلى تقليل عدد طلبات HTTP ولكنه يزيد
فرصة إخفاقات (مثل إخفاقات الاتصال) يمكن أن تحدث عندما تكون
عمليات التحميل.
يتطابق استخدام تحميل الوسائط المباشر مع ما هو موضح أعلاه
تحميل وسائط قابل للاستئناف، بالإضافة إلى المكالمة التالية لإعلام MediaHttpUploader
لإجراء عمليات تحميل مباشرة فقط:
mediaHttpUploader.setDirectUploadEnabled(true);
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 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\u003eThis document explains how to upload media to Google APIs using the Java Client Library, focusing on resumable and direct upload methods.\u003c/p\u003e\n"],["\u003cp\u003eResumable media upload is recommended for large files, allowing uploads in chunks and offering better resilience to network interruptions, similar to Google Drive's approach.\u003c/p\u003e\n"],["\u003cp\u003eDirect media upload sends the entire file in a single request, suitable for smaller files but with a higher risk of failure for larger ones.\u003c/p\u003e\n"],["\u003cp\u003eThe Java Client Library provides \u003ccode\u003eMediaHttpUploader\u003c/code\u003e and \u003ccode\u003eMediaHttpProgressListener\u003c/code\u003e for managing uploads, with built-in support for resumable uploads and an option to enable direct uploads.\u003c/p\u003e\n"],["\u003cp\u003eCode samples demonstrate how to use both resumable and direct uploads with the library, including progress tracking and handling potential errors.\u003c/p\u003e\n"]]],[],null,["# Direct and Resumable Media Uploads\n\n\u003cbr /\u003e\n\nThis document describes how to use direct and resumable media uploads with\nthe Google API Client Library for Java.\n\n\u003cbr /\u003e\n\nResumable media upload\n----------------------\n\nWhen you upload a large media file to a server, use *resumable media upload* to\nsend the file chunk by chunk. The Google API generated libraries contain\nconvenience methods for interacting with resumable media upload.\n\nThe resumable media upload protocol is similar to the resumable media upload\nprotocol described in the [Google Drive API documentation](https://developers.google.com/drive/web/manage-uploads#resumable).\n\nProtocol design\n---------------\n\nThe following sequence diagram shows how the resumable media upload protocol works:\n\n\nImplementation details\n----------------------\n\nThe main classes of interest are\n[MediaHttpUploader](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/media/MediaHttpUploader.html)\nand [MediaHttpProgressListener](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/media/MediaHttpUploaderProgressListener.html).\n\nIf methods in the service-specific generated libraries contain the `mediaUpload`\nparameter in the [Discovery document](https://developers.google.com/discovery/v1/using),\nthen a convenience method is created for these methods that takes an\n[InputStreamContent](https://googleapis.dev/java/google-http-client/latest/com/google/api/client/http/InputStreamContent.html)\nas a parameter. (For more about using media upload with the Google APIs\nDiscovery Service, see\n[Media upload](https://developers.google.com/discovery/v1/using#discovery-doc-methods-mediaupload).)\n\nFor example, the `insert` method of the [Drive API](https://developers.google.com/drive/api/v2/reference)\nsupports `mediaUpload`, and you can use the following code to upload a file: \n\n```java\nclass CustomProgressListener implements MediaHttpUploaderProgressListener {\n public void progressChanged(MediaHttpUploader uploader) throws IOException {\n switch (uploader.getUploadState()) {\n case INITIATION_STARTED:\n System.out.println(\"Initiation has started!\");\n break;\n case INITIATION_COMPLETE:\n System.out.println(\"Initiation is complete!\");\n break;\n case MEDIA_IN_PROGRESS:\n System.out.println(uploader.getProgress());\n break;\n case MEDIA_COMPLETE:\n System.out.println(\"Upload is complete!\");\n }\n }\n}\n\nFile mediaFile = new File(\"/tmp/driveFile.jpg\");\nInputStreamContent mediaContent =\n new InputStreamContent(\"image/jpeg\",\n new BufferedInputStream(new FileInputStream(mediaFile)));\nmediaContent.setLength(mediaFile.length());\n\nDrive.Files.Insert request = drive.files().insert(fileMetadata, mediaContent);\nrequest.getMediaHttpUploader().setProgressListener(new CustomProgressListener());\nrequest.execute();\n```\n\nYou can also use the resumable media upload feature without the service-specific\ngenerated libraries. Here is an example: \n\n```java\nFile mediaFile = new File(\"/tmp/Test.jpg\");\nInputStreamContent mediaContent =\n new InputStreamContent(\"image/jpeg\",\n new BufferedInputStream(new FileInputStream(mediaFile)));\nmediaContent.setLength(mediaFile.length());\n\nMediaHttpUploader uploader = new MediaHttpUploader(mediaContent, transport, httpRequestInitializer);\nuploader.setProgressListener(new CustomProgressListener());\nHttpResponse response = uploader.upload(requestUrl);\nif (!response.isSuccessStatusCode()) {\n throw GoogleJsonResponseException(jsonFactory, response);\n}\n```\n\n\u003cbr /\u003e\n\nDirect media upload\n-------------------\n\nResumable media upload is enabled by default, but you can disable it and use\ndirect media upload instead, for example if you are uploading a small file. Direct\nmedia upload was introduced in the [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 upload uploads the whole file in one HTTP request, as opposed to\nthe resumable media upload protocol, which uploads the file in multiple requests.\nDoing a direct upload reduces the number of HTTP requests but increases the\nchance of failures (such as connection failures) that can happen with large\nuploads.\n\nThe usage for direct media upload is the same as what is described above for\nresumable media upload, plus the following call that tells [MediaHttpUploader](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/media/MediaHttpUploader.html)\nto only do direct uploads: \n\n```java\nmediaHttpUploader.setDirectUploadEnabled(true);\n```"]]