java - 从 Gcloud 存储下载的图像或 Pdf 文件已损坏

标签 java google-cloud-storage mandrill gcloud-java

我正在使用从 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/

相关文章:

java - 如何模拟私有(private)静态内部类?

java - Wicket IFormValidator - 将多个 FormComponent 标记为无效

java - MathQuiz 在 java 中使用线程

google-cloud-storage - 小文件的谷歌云存储成本效益?

google-cloud-storage - 使用 Deno 将文件上传到 Google 存储

google-bigquery - GCP 上的 BigQuery 和存储有什么区别?

php - Mandrill PHP 无法获得本地颁发者 SSL 证书

java - 解析 JSON 时出错

mandrill web-hooks 打开事件 "list of opens"

grails - 测试Spock Mandril Grails v2.4.3时出错