java—是否可以在web浏览器中打开.pdf文件而不是下载?

luaexgnf  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(481)

到目前为止,我使用签名的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");
cgyqldqp

cgyqldqp1#

在生成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());

相关问题