ดาวน์โหลดสื่อที่กลับมาเล่นต่อได้
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เมื่อดาวน์โหลดไฟล์สื่อขนาดใหญ่จากเซิร์ฟเวอร์ ให้ใช้ ดาวน์โหลดสื่อต่อได้เพื่อดาวน์โหลดไฟล์ทีละส่วน Google API ไลบรารีที่สร้างขึ้นมีวิธีการอำนวยความสะดวกในการโต้ตอบกับระบบที่ดำเนินการต่อได้ ดาวน์โหลดสื่อ
โปรโตคอลการดาวน์โหลดสื่อที่กลับมาทำงานต่อได้จะคล้ายกับการอัปโหลดสื่อที่ดำเนินการต่อได้ ซึ่งอธิบายไว้ใน เอกสารประกอบของ Google Drive API
รายละเอียดการใช้งาน
คลาสหลักๆ ที่สนใจคือ MediaHttpDownloader และ MediaHttpDownloaderProgressListener เนื้อหาสื่อจะมีการดาวน์โหลดเป็นส่วนๆ และสามารถกำหนดค่าขนาดกลุ่มได้ หากมี พบข้อผิดพลาดของเซิร์ฟเวอร์ในคำขอ จากนั้นจะมีการส่งคำขออีกครั้ง
หากเมธอดในไลบรารีที่สร้างขึ้นเฉพาะบริการสนับสนุนการดาวน์โหลดใน เอกสารการค้นพบ ตามด้วย จะมีการสร้างวิธีการดาวน์โหลดที่สะดวกขึ้นสำหรับวิธีการเหล่านี้ที่จะต้องใช้ OutputStream (สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการใช้การดาวน์โหลดสื่อกับบริการค้นพบ API ของ Google โปรดดู การดาวน์โหลดสื่อ)
เช่น
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);
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 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-07-26 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);"]]