【问题标题】:is it possible to open the .pdf file in web browser instead of downloading?是否可以在网络浏览器中打开 .pdf 文件而不是下载?
【发布时间】:2021-03-06 21:45:51
【问题描述】:

截至目前,我使用签名的 url 技术从 GCS 获取文件

BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, gcsFolderPrefix + fullFileName)).build();
URL url = storage.signUrl(blobInfo, 15, TimeUnit.MINUTES,Storage.SignUrlOption.withV4Signature());

在浏览器中点击生成的签名 url 后,它会被下载,但我需要打开文件,我尝试添加响应头,如下所示:

response.addHeader(HttpHeaders.CONTENT_TYPE, "multipart/form-data");
response.addHeader(HttpHeaders.CONTENT_DISPOSITION,"inline;filename=" + fileId.toString()+".pdf");

【问题讨论】:

标签: java google-cloud-platform google-cloud-storage


【解决方案1】:

在生成 SignedUrl 之前,您需要在 GCS 中更新 blob 的元数据

BlobInfo blobinfo = BlobInfo.newBuilder(BlobId.of(bucketName, gcsFolderPrefix + fullFileName))
//Add the correct content type
.setContentType("application/pdf")
.build();

// Update the Blob
storage.update(blobinfo);

URL url = storage.signUrl(blobInfo, 15, TimeUnit.MINUTES,Storage.SignUrlOption.withV4Signature());

【讨论】:

  • 太棒了!!!!更新 blob 对象后,它开始工作了,非常感谢你
猜你喜欢
  • 2016-09-11
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 2019-03-08
  • 1970-01-01
  • 2020-11-03
相关资源
最近更新 更多