Using the Java API to download a file from Google Drive

I am using the Java API to create an application. However, I cannot crack the problem of downloading a file from disk.

I am trying to use the function indicated on the Google Developer page. (Link below)

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

However, it is unclear how to get / generate downloadURI for a specific file. And also I am confused how to upload a file using downloadURI.

I use the following function -

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) 
      {
        e.printStackTrace();
        return null;
      }
    } 
    else 
    {
     return null;
    }
 }

Here I cannot get which file should be the input parameter for the function. Please help me.

+2
source share
3 answers

. , . , .

    private void downloadRevisions(final RevisionList revisions,
            final Drive service) {

        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {

                    //TODO for debugging
                    int i = 0;

                    for (Revision revision : revisions.getItems()) {

                        System.out.println("Thread running: " + i);
                        if (revision.getExportLinks() != null
                                && revision.getExportLinks().get("text/plain") != null 
                                && revision.getExportLinks().get("text/plain").length()  > 0) {
                            HttpResponse resp = service
                                    .getRequestFactory()
                                    .buildGetRequest(
                                            new GenericUrl(revision
                                                    .getExportLinks().get("text/plain")))
                                    .execute();

                            //InputStream inputStream = resp.getContent();

                            //File Name
                            String revisionFileName = docId+"_"+revision.getId()+"_"+revision.getModifiedDate();
                            FileOutputStream outputstream = new FileOutputStream(new java.io.File(revisionFileDir, revisionFileName));
                            IOUtils.copy(resp.getContent(), outputstream,true);
                            outputstream.close();

                            // writeToFile(inputStream);
                            System.out.println("downloading: " + revisionFileName);
                        }

                        //TODO for debugging
                        i++;
                    }



                    System.out.println("Downloading Done. Document Id: "+docId);

                } catch (IOException e) {
                    Log.info("WriteToFile Fail", e.toString());
                    e.printStackTrace();
                }finally{
                    //if( doneFW !=null)
                    //  doneFW.close();
                }
            }
        });
        thread.start();
    }
0

In your calling method, set the file location and URL

eg:

com.google.api.services.drive.model.File myFile = new com.google.api.services.drive.model.File();
myFile.setDownloadUrl("https://drive.google.com/drive/u/0/folders/<encrypted_id>");
myFile.setId("<encrypted_id>");
Drive service = getDriveService(); // Assuming you have a method to authorize and get drive
BufferedReader br = new BufferedReader(new InputStreamReader(downloadFile(service,myFile));
....
....
...

Note. To get the URL, you will go to the file and right-click on it and select "Get Link"

0
source

All Articles