I am using below code to download the pdf files from the google drive using GDrive v2 api.

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. In google developers Console they have mentioned to use the below line

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. 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. That's why you are getting NPE on getting export links. You can download the file by placing a Http Get request through API directly as shown below

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. 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.

Note : I got to this detailed answer after discussion with OP in chat. To make it simple, OP used ByteArrayOutputStream instead of FileOutputStream to create the pdf file. Hope this helps someone passes by!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top