You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java从公开Google Drive URL下载文件及目录?

Java从公开Google Drive链接下载文件/目录的解决方案

一、普通URL下载报400错误的原因

你使用的是Google Drive的网页浏览链接,这类链接用于网页端查看文件,并非直接的资源下载链接。直接请求这类链接会触发服务器的验证逻辑,导致返回400错误。

二、单个公开文件的下载方案

方法1:转换为直接下载链接

  1. 提取文件ID:从公开链接中获取文件ID,例如链接https://drive.google.com/file/d/XXXXXX/view?usp=sharing里的XXXXXX就是文件ID。
  2. 构造下载链接:使用格式https://drive.google.com/uc?export=download&id=文件ID生成直接下载链接。
  3. 优化下载代码:替换原链接并优化流处理,避免资源泄漏和大文件适配问题:
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.net.URL;

public class DriveFileDownloader {
    public static void main(String[] args) {
        String fileId = "你的文件ID";
        String downloadUrl = "https://drive.google.com/uc?export=download&id=" + fileId;
        String savePath = "/Users/abhijeetkunwar/file.png";

        try (BufferedInputStream in = new BufferedInputStream(new URL(downloadUrl).openStream());
             FileOutputStream out = new FileOutputStream(savePath)) {

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            System.out.println("文件下载完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

方法2:使用Google Drive API(稳定批量场景)

利用你已掌握的Google Drive库,通过文件ID直接调用API下载:

// 假设已初始化Drive服务实例(Drive drive = ...)
String fileId = "从链接提取的文件ID";
String savePath = "/Users/abhijeetkunwar/file.png";

try (OutputStream outputStream = new FileOutputStream(savePath)) {
    drive.files().get(fileId)
            .executeMediaAndDownloadTo(outputStream);
    System.out.println("文件下载完成");
} catch (Exception e) {
    e.printStackTrace();
}

注意:公开文件无需用户授权,只需确保Drive服务实例使用API密钥或无授权模式初始化。

三、公开目录的下载方案

Google Drive无直接目录下载链接,需通过API遍历目录文件后逐个下载:

  1. 提取目录ID:从目录链接https://drive.google.com/drive/folders/YYYYYY中获取YYYYYY作为目录ID。
  2. 遍历并下载目录文件:
// 假设已初始化Drive服务实例
String folderId = "目录ID";
String saveDir = "/Users/abhijeetkunwar/downloads/";

try {
    FileList result = drive.files().list()
            .setQ("'" + folderId + "' in parents and trashed=false")
            .setFields("files(id, name)")
            .execute();

    if (result.getFiles() != null) {
        for (File file : result.getFiles()) {
            String filePath = saveDir + file.getName();
            try (OutputStream outputStream = new FileOutputStream(filePath)) {
                drive.files().get(file.getId())
                        .executeMediaAndDownloadTo(outputStream);
                System.out.printf("已下载:%s\n", file.getName());
            }
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

四、注意事项

  • 确保目标文件/目录已设置为**“知道链接的任何人都可以查看”**的公开权限,否则无法下载。
  • 超大文件下载时,网页下载链接可能触发验证页面,此时API方式更可靠。
  • 禁止直接使用包含folders、view的网页链接发起下载请求。

内容的提问来源于stack exchange,提问作者abhijeet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 14:20:27