gpt4 book ai didi

Java Google Drive 下载性能问题

转载 作者:行者123 更新时间:2023-12-01 06:11:46 25 4
gpt4 key购买 nike

我从 Google 云端硬盘下载一些文件时遇到性能问题。

以下代码在具有不同文件的 4 个并行线程中执行。

serverfile = _drive.files().get(fileid).execute();
outstream = java.nio.file.Files.newOutputStream(tempPath.getPath());

Files.Get request = _drive.files().get(serverfile.getId());
request.executeMediaAndDownloadTo(outstream);

我还尝试了不同的实现

serverfile = _drive.files().get(fileid).execute();
outstream = java.nio.file.Files.newOutputStream(tempPath.getPath());

URL url = new URL(serverfile.getDownloadUrl());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", "Bearer " + accesstoken);

IOUtils.copy(connection.getInputStream(), outstream);
connection.disconnect();

此代码会导致我的四核 Macbook Pro 上的 CPU 负载为“250%-300%”。如果我尝试使用 dropbox api 执行类似的任务,CPU 负载会低得多。

有什么可以优化以大幅减少 CPU 负载吗?

我发现的一件事是它与下载速度相关。我有 100Mbit/s 的互联网连接,如果我将速度限制为例如2 MBit/s CPU 使用率低于 10%。

最佳答案

您可以尝试使用普通的旧式 Java 下载媒体,从而绕过 Google 库。执行 files.get() 来获取 File 对象。然后使用其 downloadUrl 属性通过简单的 Java http 请求获取媒体。

关于Java Google Drive 下载性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33017339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com