简体   繁体   English

从Java驱动器的Google Drive下载pdf和tif文件

[英]Download pdf and tif file from google drive in java

I am using below code to download the pdf files from the google drive using GDrive v2 api . 我正在使用下面的代码使用GDrive v2 api从Google驱动器下载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;
        }
    }

I am getting alert saying "Corrupt or unsupported file format" when I am opening the downloaded pdf file. 打开下载的pdf文件时,我收到警告,提示“文件格式错误或不受支持”。 In google developers Console they have mentioned to use the below line 在Google Developers Console中,他们提到要使用以下行

String downloadUrl = file.getExportLinks().get("application/pdf");

But when I am using the above line I am getting null pointer exception. 但是当我使用上面的行时,我得到了空指针异常。 Can ANyone please tell me how to replace the above line in my code to download the pdf file and open the file. ANyone能否告诉我如何替换代码中的上述行以下载pdf文件并打开文件。 And also I want to know whether the file from google drive has been completely downloaded. 我也想知道是否从谷歌驱动器的文件已经完全下载。

Please help me. 请帮我。

Thanks in advance. 提前致谢。

You don't have to go for export options in the API since the pdf file is just available in the drive and there is no conversion happening from Google doc format to any convertion format like pdf. 您无需在API中使用导出选项,因为pdf文件仅在驱动器中可用,并且不会发生从Google doc格式到pdf等任何转换格式的转换。 That's why you are getting NPE on getting export links. 这就是为什么您在获取导出链接时得到NPE的原因。 You can download the file by placing a Http Get request through API directly as shown below 您可以通过直接通过API发出Http Get请求来下载文件,如下所示

private static InputStream downloadFile(Drive service, File file) { 
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) { 
try { 
HttpResponse resp = 
service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())) 
.execute(); 
return resp.getContent(); 
} catch (IOException e) { 
// An error occurred. 
e.printStackTrace(); 
return null; 
} 
} else { 
// The file doesn't have any content stored on Drive. 
return null; 
} 
}

You have to create a FileOutputStream instead of ByteArrayOutputStream to create a file with the InputStream . 您必须创建FileOutputStream而不是ByteArrayOutputStream才能使用InputStream创建文件。 Below snippet may help 以下代码段可能会有所帮助

OutputStream outputStream = new FileOutputStream(new File("/home/input.pdf")); 
int read = 0; 
byte[] bytes = new byte[1024]; 
while ((read = inputStream.read(bytes)) != -1) { 
outputStream.write(bytes, 0, read); 
}

Ensure that you flush the OutputStream and close both the streams after use. 使用后,请确保刷新OutputStream并关闭两个流。

Note : I got to this detailed answer after discussion with OP in chat. 注意:在与OP聊天讨论后,我得到了这个详细的答案。 To make it simple, OP used ByteArrayOutputStream instead of FileOutputStream to create the pdf file. 为简单ByteArrayOutputStream ,OP使用ByteArrayOutputStream而不是FileOutputStream来创建pdf文件。 Hope this helps someone passes by! 希望这可以帮助某人路过!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM