简体   繁体   中英

download the files from Gdrive to local system by using java

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.

you can use google drive api and send Http get request, you can see this tutorial

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

Thanks Hanan it works fine.By using the retrieveAllFiles() i can list all the documents.And i have stored the retrieved documents in my local by using this below code.Is it a correct way to download.

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

It stores all the documents in local. The text (.txt) files are open properly in my local, but the image files or pdf files are not open properly.It gives some error messages like file corrupted. There is no content in the image or pdf documents how can i get content and store it. Any suggestions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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