Google云端硬盘API无法下载文件(Java v3) [英] Google Drive API not downloading FIles (Java v3)

查看:234
本文介绍了Google云端硬盘API无法下载文件(Java v3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从这里使用代码: https://developers.google.com/drive/api/v3/manage-downloads#downloading_a_file

I'm using the code from here: https://developers.google.com/drive/api/v3/manage-downloads#downloading_a_file

我正在使用的代码段如下,并放置在main方法中:

The code snippet I'm using is the following and placed in the main method:

    String fileId = "some file ID";
    OutputStream outputStream = new ByteArrayOutputStream();
    driveService.files().get(fileId)
    .executeMediaAndDownloadTo(outputStream);

我没有发现实际下载文件的代码的迹象,如果实际下载文件,我也不知道文件在哪里. 我不确定我是否使用适当的范围来获得下载文件的许可.只要知道fileID,我就可以上传,列出和删除文件,但是下载似乎不起作用.

I have found no sign of the code actually downloading the file, nor do I know where the file is IF it actually downloads. I'm not sure if I am using the proper scope to gain permission to download files. I am able to upload, list, and delete files as long as I know the fileID, but downloading seems to not work.

    private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);

或者,我正在尝试创建一种方法来制定下载协议,如下所示:

Alternatively, I'm trying to create a method to enact the download protocol like so:

    private static void downloadFile(Drive service, File file (or String fileID)){
    }

但不确定如何执行此操作.我尝试过在线查找示例,但大多数都是从v1或v2 api中获取的,似乎对我不起作用.

but am not sure on how to do so. I've tried looking for samples online but most are from v1 or v2 apis and don't seem to work for me.

此外,我在某处读到无法下载文件夹.相反,我必须一个接一个地下载文件夹中的每个项目. 那么,我是否必须对fileID进行Arraylist/list/array的初始化,然后在初始化一个表示fileID的变量之后对其进行遍历?

Also, I've read somewhere that it is not possible to download a Folder. Instead, I have to download each item in the folder one by one. So do I have to make an Arraylist/list/array of the fileIDs and iterate through it after initializing a variable to represent fileID?

已经取得了一些进展,但是我仍然想解决一些问题.

Some progress has been made, but I still have some problems I'm trying to thrash out.

    List<File> files = result.getFiles();
    File newFile;
    if (files == null || files.isEmpty()) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:");
        for (File file : files) {
            System.out.printf("%s (%s)\n", file.getName(), file.getId());
            String fileId = file.getId();
            //System.out.println(fileId);
            String fileName = file.getName();
            //System.out.println(fileName);
            OutputStream outputstream = new FileOutputStream();
            service.files().get(fileId)
            .executeMediaAndDownloadTo(outputstream);
            outputstream.flush();
            outputstream.close();
    }

我想要什么: 上面的代码在main方法中.我不知道这是否是正确的方法,但是当程序获取每个文件并执行System.out.printf时,我还希望它下载该文件(具有相同的mimeType和相同的名称也是如此)到在OutputStream构造函数中设置的目标位置(C://User//某些名称//Downloads).

What I want: The above code is in the main method. I don't know if this is the proper way to do it, but as the program fetches each file and executes the System.out.printf, I also want it to download that file (with the same mimeType, and pref the same name too) into the destination set in the OutputStream constructor (C://User//some name//Downloads).

我尝试过的方法: 根据我的测试,它仅按照我想要的方式下载第一个文件,但这仅是因为我在OutputStream中指定了名称和扩展名.我已经初始化了变量'fileId'和'fileName',以便当程序获取下一个文件的元数据时,它们将根据信息而变化,但是我不知道如何在此代码中更改或设置多个构造函数:

What I've tried: From what I've tested, it only downloads the first file exactly the way I want, but only because I specify the name and extension in OutputStream. I've initialized variables 'fileId' and 'fileName' so that they will change according to the info as the program fetches the metadata for the next file, but I don't know how to change or set multiple constructors into this code:

    OutputStream outputstream = new FileOutputStream();
            service.files().get(fileId)
            .executeMediaAndDownloadTo(outputstream);

下载所有文件.

我在Google云端硬盘中的文件夹层次结构如下:

My folder hierarchy in Google Drive is like this:

日志

-bin(文件夹)

----一堆.bin文件

---- bunch of .bin files

-.xml文件

-.xml文件

推荐答案

您正在使用

You are using a ByteArrayOutputStream object as the output of your download. If your program terminates without having saved the contents of this object somewhere, you will not be able to find this information in your computer's disk, as it is not written to it but rather saved in memory as a buffered byte-array (refer to the previous link for more information).

如果要将下载的输出保存到文件中,建议您使用

If you want to save the output of the download to the file, I suggest you use instead a FileOutputStream as the destination of your download. In order to do that, you have to modify your code as follows:

  1. 添加适当的import声明:

import java.io.FileOutputStream;

  • 修改您的outputStream变量分配,如下所示:

  • Modify your outputStream variable assignment as follows:

    OutputStream outputStream = new FileOutputStream('/tmp/downloadedfile');
    

    传递给FileOutputStream的参数应该是下载所需的目标路径.

    Where the parameter passed to FileOutputStream should be the desired destination path of your download.

    outputStream.flush();
    outputStream.close();
    

    这样可以确保文件被正确写入.

  • This will ensure that your file is being written to properly.

    关于下载文件夹,您完全正确-首先需要获取要下载的文件夹及其每个子级.为了更好地理解操作方法,建议您检查以下答案:下载Google Drive API的文件夹

    In regards to downloading a folder, you are completely right - you will first need to fetch the folder you want to download, and each of their children. In order to better understand how to do it, I suggest you check out the following answer: Download folder with Google Drive API

    String destinationFolder = "/tmp/downloadedfiles/";
    List<File> files = result.getFiles();
    File newFile;
    if (files == null || files.isEmpty()) {
      System.out.println("No files found.");
    } else {
      System.out.println("Files:");
      for (File file : files) {
        System.out.printf("%s (%s)\n", file.getName(), file.getId());
        String fileId = file.getId();
        String fileName = file.getName();
        OutputStream outputstream = new FileOutputStream(destinationFolder + fileName);
        service.files().get(fileId)
               .executeMediaAndDownloadTo(outputstream);
        outputstream.flush();
        outputstream.close();
      }
    }
    
    

    这篇关于Google云端硬盘API无法下载文件(Java v3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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