首页 > 解决方案 > Google Drive API V3 下载返回 416 错误

问题描述

我在 Google 驱动器中有一个 bin 子文件夹,其中包含 1500-1600 个 bin 文件,存储空间约为 1 GB。我的程序在 544 个文件处停止下载,我不知道为什么。这是我在 Google API 控制台中的限制:

这是编译bin文件列表的方法。

public static final List<File> getbinfolderlist(Drive service, String bin_folder_id) throws IOException {
        try {
            List<File> list = new ArrayList<File>();
            String pageToken = null;
            do {
                FileList result = service.files().list()
                    .setQ("'" + bin_folder_id + "' in parents")
                    .setPageToken(pageToken)
                    .execute();
                    for (File file : result.getFiles()) {
                        list.add(file);
                    }
                    pageToken = result.getNextPageToken();
                } while (pageToken != null);
                return list;
        } catch (Exception e) {
            return null;
        }
    }

在 main 方法中运行它:

List<File> bin_files = getbinfolderlist(service, bin_folder_id);
System.out.println(bin_files.size());

返回 bin 文件夹中文件的确切数量,因此我知道列出了所有 bin 文件。

遍历列表并下载bin文件的方法如下:

public static void downloadBinFiles(Drive service, List<File> bin_files, String dst_dir) {
        try {
            int i = 0;
            for (File file : files) {
                String fileId = file.getId();
                String fileName = file.getName();
                //need to check if folder exists. If not, create folder.
                System.out.println("File downloading is: " + fileName);
                OutputStream outputstream = new FileOutputStream(dst_dir + fileName);
                    service.files().get(fileId)
                        .executeMediaAndDownloadTo(outputstream);
                    outputstream.flush();
                    outputstream.close();
                i++;
                System.out.println(i + " files downloaded.");
                }
        } catch (Exception e) {
            Console console = System.console();
            console.writer().println("Error.");
        }
    }

并且由于某种原因,它总是在文件 544 处出现“错误”。是否有一些我正在达到的配额我不知道并且需要增加?代码中有什么东西吗?我正在使用此命令在 main 方法中执行 bin 文件的下载

downloadBinFiles(service, bin_files, bin_path );

我得到的错误信息是这样的:

com.google.api.client.http.HttpResponseException: 416 Requested range not satisfiable
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "requestedRangeNotSatisfiable",
    "message": "Request range not satisfiable"
   }
  ],
  "code": 416,
  "message": "Request range not satisfiable"
 }
}

        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1070)
        at com.google.api.client.googleapis.media.MediaHttpDownloader.executeCurrentRequest(MediaHttpDownloader.java:245)
        at com.google.api.client.googleapis.media.MediaHttpDownloader.download(MediaHttpDownloader.java:199)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeMediaAndDownloadTo(AbstractGoogleClientRequest.java:562)
        at com.google.api.services.drive.Drive$Files$Get.executeMediaAndDownloadTo(Drive.java:3256)
        at DriveQuickstart.downloadBinFiles(DriveQuickstart.java:223)
        at DriveQuickstart.main(DriveQuickstart.java:127)

编辑:如果这是相关的,bin 文件的大小范围从 < 0 KB 到略低于 14,000 KB。文件夹大小为 843 MB。

标签: javagoogle-apigoogle-drive-api

解决方案


推荐阅读