我正在使用从 Gcloud 存储下载的文件作为 Mandrill API 的附件,以便作为电子邮件中的附件发送。问题是它仅适用于文本文件,但对于图像或 Pdf,附件已损坏。 以下代码用于下载文件并将其转换为 Base64 编码的字符串。
Storage.Objects.Get getObject = getService().objects().get(bucket, object);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// If you're not in AppEngine, download the whole thing in one request, if possible.
getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true);
getObject.executeMediaAndDownloadTo(out);
//log.info("Output: {}", out.toString("UTF-8"));
return Base64.encodeBase64URLSafeString(out.toString("UTF-8")
.getBytes(StandardCharsets.UTF_8));
我在 Mandrill API 的 MessageContent 内容中设置此字符串。
最佳答案
成功了。我只需要将 OutputStream 存储在临时文件中,然后将其用作电子邮件中的附件。贴出下面的代码供引用。
Storage.Objects.Get getObject = storage.objects().get("bucket", "object");
OutputStream out = new FileOutputStream("/tmp/object");
// If you're not in AppEngine, download the whole thing in one request, if possible.
getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true);
getObject.executeMediaAndDownloadTo(out);
关于java - 从 Gcloud 存储下载的图像或 Pdf 文件已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35415755/