Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Mẫu sau đây cho thấy cách truy xuất một ghi chú và tệp đính kèm của ghi chú đó:
REST
Gọi media.download() bằng tên của tệp đính kèm tải xuống và tham số URL alt=media.
Tham số URL alt=media cho máy chủ biết rằng một yêu cầu tải nội dung xuống đang được thực hiện.
Để lấy tên của tệp đính kèm, trước tiên, bạn phải truy xuất ghi chú.
Java
/** * Gets and downloads the attachment of a note. * * @param note The note whose attachment will be downloaded. * @throws IOException */privatevoidgetNoteAttachment(Notenote)throwsIOException{// First call is to get the attachment resources on the note.List<Attachment>attachments=keepService.notes().get(note.getName()).execute().getAttachments();if(!attachments.isEmpty()){Attachmentattachment=attachments.get(0);StringmimeType=attachment.getMimeType().get(0);// A second call is required in order to download the attachment with the specified mimeType.OutputStreamoutputStream=newFileOutputStream("attachmentFile."+mimeType.split("/")[1]);keepService.media().download(attachment.getName()).setMimeType(mimeType).executeMediaAndDownloadTo(outputStream);}}
[null,null,["Cập nhật lần gần đây nhất: 2025-03-11 UTC."],[],[],null,["# Retrieve notes and attachments\n\nThe following sample shows how to retrieve a note and its attachment: \n\n### REST\n\nCall\n[`media.download()`](/workspace/keep/api/reference/rest/v1/media/download)\nwith the name of the attachment download and the `alt=media` URL parameter.\nThe `alt=media` URL parameter tells the server that a download of content is\nbeing requested.\n\nTo get the name of the attachment, you must first [retrieve the note](#note).\n| **Note:** The `alt=media` URL parameter is a [system parameter](https://cloud.google.com/apis/docs/system-parameters) available across all Google REST APIs. If you use a client library for the Google Keep API, you do not need to explicitly set this parameter.\n\n### Java\n\n /**\n * Gets and downloads the attachment of a note.\n *\n * @param note The note whose attachment will be downloaded.\n * @throws IOException\n */\n private void getNoteAttachment(Note note) throws IOException {\n // First call is to get the attachment resources on the note.\n List\u003cAttachment\u003e attachments =\n keepService.notes().get(note.getName()).execute().getAttachments();\n\n if (!attachments.isEmpty()) {\n Attachment attachment = attachments.get(0);\n String mimeType = attachment.getMimeType().get(0);\n // A second call is required in order to download the attachment with the specified mimeType.\n OutputStream outputStream = new FileOutputStream(\"attachmentFile.\" + mimeType.split(\"/\")[1]);\n keepService\n .media()\n .download(attachment.getName())\n .setMimeType(mimeType)\n .executeMediaAndDownloadTo(outputStream);\n }\n }"]]