使用Java API从Google Drive下载文件 [英] Using Java APIs for downloading file from the Google Drive

查看:293
本文介绍了使用Java API从Google Drive下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java API来创建应用程序。但是我无法解决从驱动器下载文件的问题。



我试图使用Google Developer页面上给出的功能。 (以下链接)



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



然而,并没有明确提及如何为特定文件获取/生成downloadURI。我也很困惑如何使用downloadURI下载文件。



我正在使用以下功能 -



<$ (file.getDownloadUrl()!= null&&& file.getDownloadUrl())的私有静态InputStream downloadFile(驱动器服务,文件文件)
{
> length()> 0)
{
try
{
HttpResponse resp = service.getRequestFactory()。buildGetRequest
(new GenericUrl(file.getDownloadUrl()) )。执行();
return resp.getContent();
}
catch(IOException e)
{
e.printStackTrace();
返回null;
}
}
else
{
return null;


$ / code $ / pre

在这里,我无法得到什么文件应该是该函数的输入参数。
请帮助我。

解决方案

在您的调用方法中,设置fileId和url位置



例如:

  com.google.api.services.drive.model.File myFile =新的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(); //假设你有一个授权和获取驱动器的方法
BufferedReader br = new BufferedReader(new InputStreamReader(downloadFile(service,myFile));
....
....
...

注意:要获取您导航到该文件的url并右键单击该文件并选择获取链接


I am using the Java APIs for creating an application. However I am not able to crack the problem of downloading a file from the drive.

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

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

However, it is not clearly mentioned how to get/generate downloadURI for a specific file. And also I am confused how to download the file using the downloadURI.

I am using 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 am not able to get what file should be the input parameter for the function. Please help me out.

解决方案

In your calling method, set fileId and url location

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 navigate to the file and right click on the file and select "Get link"

这篇关于使用Java API从Google Drive下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆