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

Java 从Gcloud存储下载的图像或Pdf文件已损坏,java,google-cloud-storage,mandrill,gcloud-java,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(); //

我使用从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);

让它工作起来。我只需要将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);

由于MessageContent.Content需要一个字符串,而不是字节数组,因此我假设您正在将base64.encodeBase64URLSafeString(out.toString(“UTF-8”))作为MessageContent传递,但在此仅使用getBytes()来测试损坏情况?是的,这就是我使用base64.encodeBase64URLSafeString的原因,但我也尝试了base64.encodeBase64URLSafeString(out.toByteArray())这就产生了同样的结果。只有文本文件被正确地传输,其余部分被破坏。我认为问题在于一次下载完整的数据。因为经过更多的测试,我发现大型文本文件也无法获取所有数据,因为某些部分被破坏。由于MessageContent.Content需要一个字符串,而不是字节数组,我假设您是传入的g base64.encodeBase64URLSafeString(out.toString(“UTF-8”))作为MessageContent,但仅在此处使用getBytes()来测试损坏?是的,这就是我使用base64.encodeBase64URLSafeString的原因,但我也尝试了base64.encodeBase64URLSafeString(out.toByteArray())这就产生了同样的结果。只有文本文件正常运行,其余部分都损坏了。我认为问题在于一次下载完整的数据。因为经过更多的测试,我发现大型文本文件也无法获取所有数据,因为某些部分损坏了。