java - 在 Google 云端硬盘中下载文件时,文件暂时移动错误

标签 java download google-drive-api chunking

我正在尝试使用以下代码使用 V3 Rest api 从 Google Drive 下载文件

public static void downloadFileAsChunksGdrive() throws IOException {
        String accessToken = "XYZ";                 
        Credential credential = new GoogleCredential().setAccessToken(accessToken);
        Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName("ABC")
                .build();
        File file = service.files().get("fileId").setFields("size, webContentLink").execute();

        String downloadUrl = file.getWebContentLink();
        System.out.println(downloadUrl);
        long fileSize = file.getSize();
        System.out.println(fileSize);
        java.io.File outputFile = new java.io.File("/A/B/C/"+ file.getName())
        FileOutputStream fos = new FileOutputStream(outputFile);

        for (long i = 0; i< fileSize; i=i+1000000) {
            byte[] fileRangeBytes = getBytes(service, downloadUrl, i, 1000000);
            fos.write(fileRangeBytes);
            fos.flush();
        }
        fos.close();
    }

    private static byte[] getBytes(Drive drive, String downloadUrl, long position, long byteCount) {
        byte[] receivedByteArray = null;
        if (downloadUrl != null && downloadUrl.length() > 0) {
            try {
                com.google.api.client.http.HttpRequest httpRequestGet = drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl));
                httpRequestGet.getHeaders().setRange("bytes=" + position + "-" + (position + byteCount - 1));
                com.google.api.client.http.HttpResponse response = httpRequestGet.execute();
                InputStream is = response.getContent();
                receivedByteArray = IOUtils.toByteArray(is);
                response.disconnect();
                System.out.println("google-http-client-1.18.0-rc response: [" + position + ", " + (position + receivedByteArray.length - 1) + "]");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return receivedByteArray;
    }

执行代码时,出现以下错误

com.google.api.client.http.HttpResponseException: 302 Moved Temporarily
<HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="https://doc-00-a0-docs.googleusercontent.com/docs/securesc/pcngq5lgspj8i9l0m7amt8h5ad9otomr/6pm24trt0vnj4ksogqjedtkfhhh7588a/1495101600000/06178136502342984870/06178136502342984870/0B08iWIDcqPjkR2VCNFNqYXlBN0E?e=download&amp;nonce=8acfbud35mah2&amp;user=06178136502342984870&amp;hash=4iv88ck5iea3ql8pbfv5gje54ufr2h8h">here</A>.
</BODY>
</HTML>

    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1061)

有人可以帮我解决这个问题吗?

最佳答案

发生这种情况是因为该页面正在尝试重定向到用户身份验证页面。

我通过使用带有“alt=media”查询参数的文件获取请求来使其工作,工作代码如下

private static byte[] getBytes(Drive drive, String accessToken, String fileId, long position, long byteCount) {
        byte[] receivedByteArray = null;
        String downloadUrl = "https://www.googleapis.com/drive/v3/files/" + fileId + "?alt=media&access_token="
                + accessToken;
        if (downloadUrl != null && downloadUrl.length() > 0) {
            try {
                com.google.api.client.http.HttpRequest httpRequestGet = drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl));
                httpRequestGet.getHeaders().setRange("bytes=" + position + "-" + (position + byteCount - 1));
                com.google.api.client.http.HttpResponse response = httpRequestGet.execute();
                InputStream is = response.getContent();
                receivedByteArray = IOUtils.toByteArray(is);
                response.disconnect();
                System.out.println("google-http-client-1.18.0-rc response: [" + position + ", " + (position + receivedByteArray.length - 1) + "]");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return receivedByteArray;
    }

关于java - 在 Google 云端硬盘中下载文件时,文件暂时移动错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44045105/

相关文章:

node.js - google-drive 无法获取推送通知

java - eclipse运行配置中的"A configuration with this name already exists"错误

java - 用 % 处理溢出

java - Postgresql JDBC 驱动程序中的批量更新在自动提交中回滚

java - 如何使用java从直播视频中获取视频链接?

file - flask 文件下载方法不允许错误

java - liquibase.exception.ChangeLogParseException : Error Reading Migration File: Found 2 files that match mychanges. xml

python - 如何加快Flask响应下载速度

java - 如何将大文件(> 5 mb)从 Blobstore 发布到 Google 云端硬盘?

python-3.x - Google Drive API Python 服务帐户示例