使用gzip压缩通过rest api从Google云端硬盘下载文件 [英] Download file from Google Drive via rest api using gzip compression

查看:89
本文介绍了使用gzip压缩通过rest api从Google云端硬盘下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从Google云端硬盘下载公开共享的文件.效果很好.

I'm using the following code to download a publicly shared file from Google drive. It works fine.

InputStream input = null;
OutputStream output = null;

HttpURLConnection httpsURLConnectionToGoogleDrive = (HttpURLConnection) new URL(downloadUrl).openConnection();
httpsURLConnectionToGoogleDrive.connect();

long fileLength = httpsURLConnectionToGoogleDrive.getContentLength();
input = httpsURLConnectionToGoogleDrive.getInputStream();

byte data[] = new byte[MediaHttpUploader.MINIMUM_CHUNK_SIZE];
int count;
while ((count = input.read(data)) != -1) {
    // allow canceling with back button
    if (isCancelled()) {
        input.close();
        return null;
    }
    total += count;
    log("Received Up To : "+ total + " ("+ count +") ");
    // publishing the progress....
    if (fileLength > 0) { // only if total length is known
        publishProgress((int) (total * 100 / fileLength));
    }
    output.write(data, 0, count);
}

现在,我想添加gzip压缩以节省带宽.从此参考中,我添加了以下代码:-

Now, I want to add gzip compression to save bandwidth. From this reference I added the following code :-

httpsURLConnectionToGoogleDrive.setRequestProperty("Accept-Encoding", "gzip");
httpsURLConnectionToGoogleDrive.setRequestProperty("User-Agent", "my program (gzip)");

但是我无法使它正常工作.我被困在这里.该怎么办?

But I could't get it to work. I'm stuck here. What to do?

推荐答案

默认情况下,Drive API已以gzip的形式管理请求,因此您无需付出额外的努力.仅当您要创建自己的库时,才应指定gzip的标头.

The Drive API already manages the requests as gzip as default, so there is no extra effort from your side. Only if you were to create you're own library, then you should specify the headers for gzip.

这篇关于使用gzip压缩通过rest api从Google云端硬盘下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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