Java 错误:无法使用google cloud StorageOptions for SignUrl加载PDF文档
我正在尝试使用google cloud SignUrl从Web API获取PDF文档,并希望在应用程序中显示。获取“错误:无法加载PDF文档” 我跟随signurl:-Java 错误:无法使用google cloud StorageOptions for SignUrl加载PDF文档,java,playframework,google-cloud-storage,Java,Playframework,Google Cloud Storage,我正在尝试使用google cloud SignUrl从Web API获取PDF文档,并希望在应用程序中显示。获取“错误:无法加载PDF文档” 我跟随signurl:- public com.google.cloud.storage.Storage getStorage() { if(storage == null) { synchronized (lock) { if(storage == null) { stor
public com.google.cloud.storage.Storage getStorage() {
if(storage == null) {
synchronized (lock) {
if(storage == null) {
storage = StorageOptions.getDefaultInstance().getService();
}
}
}
return storage;
}
// -- TRY other way to get pdf file.
public String getSignedUrl(String fileName) throws IOException {
BlobId blobId = BlobId.of(gcsBucket, remotePath);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
/*.setContentType("application/doc")*/
/*.setContentType("application/pdf")*/
/*.setContentType("application/octet-stream")*/
.build();
URL url = cloudStorageFactory.getStorage().signUrl(blobInfo,ttlMinutes, TimeUnit.MINUTES, SignUrlOption.httpMethod(HttpMethod.GET)
// ,SignUrlOption.withContentType()
);
return url.toString();
}
下面的代码可以很好地用于图像文件
我试着用代码注释。
它的代码获取所有文件和pdf文件的signurl,但我无法弄清楚它是如何获取pdf文件以供查看的
请检查我的密码:
控制器类:-
class ControllerClass{
public Result show(String fileName) throws IOException {
return redirect(mModelService.getSignedUrl(fileName));
// -- TRY other way to get pdf file.
//return redirect(mModelService.getSignedUrl(userId,fileName));
// .withHeader("Content-Type","application/pdf");
// .as("application/octet-stream");
}
}
获取签名Url:-
public String getSignedUrl(String fileName) throws IOException {
String remotePath = filePath(fileName);
BlobId blobId = BlobId.of("gcsBucket", remotePath);
Blob blob = getStorage().get(blobId);
if(blob != null && blob.exists()){
URL url = blob.signUrl(ttlMinutes, TimeUnit.MINUTES, SignUrlOption.httpMethod(HttpMethod.GET)
);
return url.toString();
}
获取存储:-
public com.google.cloud.storage.Storage getStorage() {
if(storage == null) {
synchronized (lock) {
if(storage == null) {
storage = StorageOptions.getDefaultInstance().getService();
}
}
}
return storage;
}
// -- TRY other way to get pdf file.
public String getSignedUrl(String fileName) throws IOException {
BlobId blobId = BlobId.of(gcsBucket, remotePath);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
/*.setContentType("application/doc")*/
/*.setContentType("application/pdf")*/
/*.setContentType("application/octet-stream")*/
.build();
URL url = cloudStorageFactory.getStorage().signUrl(blobInfo,ttlMinutes, TimeUnit.MINUTES, SignUrlOption.httpMethod(HttpMethod.GET)
// ,SignUrlOption.withContentType()
);
return url.toString();
}
您不知道如何将PDF文件从控制器发送到视图?获取文件时遇到问题。或者两者都有?这个错误是从哪里来的?什么是错误堆栈?获取文件时。首先,文件是否存在?您正在使用哪个Java IO功能访问它?是的,文件存在。它不是用于存储临时文件的任何IO,它的代码使用注册url,直接显示文件的有效显示请参见此行:-url url url=cloudStorageFactory.getStorage().signUrl(blobInfo,ttlMinutes,TimeUnit.MINUTES,SignUrlOption.httpMethod(httpMethod.GET);