【发布时间】:2014-05-06 10:16:15
【问题描述】:
我正在使用以下代码使用 GDrive v2 api 从谷歌驱动器下载 pdf 文件。
public InputStream downloadFile(Drive service, String fileId) {
try {
System.out.println("From Down load file file id" + fileId);
System.out.println("From Down load file file id" + service);
File file = service.files().get(fileId).execute();
String downloadUrl = file.getDownloadUrl();
if (downloadUrl != null && downloadUrl.length() > 0) {
HttpResponse resp = service.getRequestFactory()
.buildGetRequest(new GenericUrl(downloadUrl)).execute();
System.out.println("resp content: " + resp.getContent());
return resp.getContent();
} else {
// The file doesn't have any content stored on Drive.
return null;
}
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
}
当我打开下载的 pdf 文件时,我收到警告说“文件格式损坏或不受支持”。在谷歌开发者控制台中,他们提到使用以下行
String downloadUrl = file.getExportLinks().get("application/pdf");
但是当我使用上面的行时,我得到了空指针异常。谁能告诉我如何替换我的代码中的上述行以下载pdf文件并打开文件。 我也想知道谷歌驱动器中的文件是否已经完全下载。
请帮帮我。
提前致谢。
【问题讨论】:
-
你检查你的文件ID是否正确?
-
如何查看文件ID?
-
您是如何获得文件 ID 的?方法
downloadFile是怎么调用的? -
String fileID = request.getParameter("fileID"); System.out.println("文件 ID" + fileID);输入流是 = null; ServletOutputStream so = null;字节[] bt = null; is = driveaccess.downloadFile(serviceGoogleAPI, fileID);
-
是的,我完全理解。您如何确认 fileID 实际上指向 Google 驱动器中的正确文件?
标签: java android pdf google-api google-drive-api