首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Java API从Google Drive下载文件

使用Java API从Google Drive下载文件
EN

Stack Overflow用户
提问于 2013-11-27 14:06:48
回答 3查看 6.1K关注 0票数 2

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

我正在尝试使用Google开发人员页面上给出的函数。(链接如下)

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

但是,对于如何获取/生成特定文件的downloadURI,并没有明确的说明。另外,我也不知道如何使用downloadURI下载文件。

我正在使用以下函数-

代码语言:javascript
运行
复制
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;
    }
 }

在这里,我不能获得哪个文件应该是函数的输入参数。请帮帮我。

EN

回答 3

Stack Overflow用户

发布于 2014-01-31 10:36:48

这就是我是如何做到的。我下载的不是文档,而是给定文档的所有修订版。所以逻辑是一样的。

代码语言:javascript
运行
复制
    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
EN

Stack Overflow用户

发布于 2015-09-30 10:34:55

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

例如:

代码语言:javascript
运行
复制
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));
....
....
...

注意:要获取url,请导航到该文件,右键单击该文件,然后选择"Get link“。

票数 0
EN

Stack Overflow用户

发布于 2013-11-27 14:32:14

看看这个

https://developers.google.com/drive/v2/reference/files/list

这将返回来自驱动器的文件列表,然后你只需要从这个列表中传递一个你想要下载的文件

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20235104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档