0

Drive Quickstart: Run a Drive App in Java example works for uploading files fine. I want to download the files from Gdrive to local system by using java. For download they are given a method

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;
    }
}

The above method,how can i give inputs? and from where i give the inputs? Can anyone give a complete code for download like Quickstart upload class.

any help will be appreciated.

4

2 回答 2

0

你可以使用 google drive api 并发送 Http 获取请求,你可以看到这个教程

https://developers.google.com/drive/manage-downloads

于 2013-07-18T10:26:00.173 回答
0

谢谢哈南,它工作正常。通过使用retrieveAllFiles(),我可以列出所有文档。并且我已经使用下面的代码将检索到的文档存储在我的本地。它是一种正确的下载方式。

for(File f:result){         
        i++;
        System.out.println("File Name==>"+f.getTitle());
        System.out.println("File Id==>"+f.getId());
        System.out.println("File ext==>"+f.getFileExtension());
        System.out.println("File size==>"+f.getFileSize());
InputStream in = downloadFile(service,f);
        byte b[] = new byte[in.available()];
        in.read(b);
        java.io.File ff = new java.io.File("/home/test/Desktop/gdocs/"+f.getTitle());
        FileOutputStream fout = new FileOutputStream(ff);
        fout.write(b);
        fout.close();
} 

它将所有文档存储在本地。文本 (.txt) 文件在我的本地正确打开,但图像文件或 pdf 文件未正确打开。它给出了一些错误消息,如文件损坏。图像或 pdf 文档中没有内容我如何获取内容并存储它。有什么建议么

于 2013-07-22T06:17:43.160 回答