การอัปโหลดสื่อโดยตรงและทำต่อได้
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เอกสารนี้จะอธิบายวิธีใช้การอัปโหลดสื่อโดยตรงและการอัปโหลดต่อได้ด้วย
ไลบรารีของไคลเอ็นต์ Google API สำหรับ Java
อัปโหลดสื่อต่อได้
เมื่อคุณอัปโหลดไฟล์สื่อขนาดใหญ่ไปยังเซิร์ฟเวอร์ ให้ใช้การอัปโหลดสื่อที่ดำเนินการต่อได้เพื่อ
ให้ส่งไฟล์ทีละส่วน ไลบรารีที่ Google API สร้างขึ้นประกอบด้วย
วิธีอำนวยความสะดวกในการโต้ตอบกับการอัปโหลดสื่อที่กลับมาทำงานต่อได้
โปรโตคอลการอัปโหลดสื่อที่ดำเนินการต่อได้คล้ายกับการอัปโหลดสื่อที่ดำเนินการต่อได้
ตามที่อธิบายไว้ในเอกสารประกอบของ Google Drive API
การออกแบบโปรโตคอล
แผนภาพลำดับต่อไปนี้แสดงวิธีการทำงานของโปรโตคอลการอัปโหลดสื่อที่กลับมาทำงานต่อได้
วันที่ 
รายละเอียดการใช้งาน
ประเภทหลักๆ ที่สนใจได้แก่
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);
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 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\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```"]]