以下示例展示了如何检索备注及其附件:
REST
使用以下代码调用 media.download()
:
附件下载的名称和 alt=media
网址参数。通过
alt=media
网址参数会告知服务器某下载内容已被下载
请求。
如需获取附件名称,您必须先检索备注。
Java
/**
* Gets and downloads the attachment of a note.
*
* @param note The note whose attachment will be downloaded.
* @throws IOException
*/
private void getNoteAttachment(Note note) throws IOException {
// First call is to get the attachment resources on the note.
List<Attachment> attachments =
keepService.notes().get(note.getName()).execute().getAttachments();
if (!attachments.isEmpty()) {
Attachment attachment = attachments.get(0);
String mimeType = attachment.getMimeType().get(0);
// A second call is required in order to download the attachment with the specified mimeType.
OutputStream outputStream = new FileOutputStream("attachmentFile." + mimeType.split("/")[1]);
keepService
.media()
.download(attachment.getName())
.setMimeType(mimeType)
.executeMediaAndDownloadTo(outputStream);
}
}