簡體   English   中英

如何使用 Java 從公開可用的谷歌驅動器 URL 下載文件?

[英]How to download files from publicly available google drive URL using Java?

我一直在嘗試使用 Java 從公開可用的谷歌驅動器鏈接 URL 下載文件/整個目錄。我能夠使用谷歌驅動器庫讀取我的谷歌驅動器中存在的文件,但我無法理解如何傳遞谷歌驅動器鏈接URL。

此外,我嘗試使用從 URL 下載文件的典型方法,但它產生了錯誤 java.io.IOException:服務器返回 HTTP 響應代碼:URL 為 400。

URL url;
URLConnection con;
DataInputStream dis;
FileOutputStream fos;
byte[] fileData;
try {
    url = new URL("https://drive.google.com/drive/folders/<some-alphanumeric-code>/<file-name>"); //File Location goes here
    con = url.openConnection(); // open the url connection.
    dis = new DataInputStream(con.getInputStream());
    fileData = new byte[con.getContentLength()];
    for (int q = 0; q < fileData.length; q++) {
        fileData[q] = dis.readByte();
    }
    dis.close(); // close the data input stream
    fos = new FileOutputStream(new File("/Users/abhijeetkunwar/file.png")); //FILE Save Location goes here
    fos.write(fileData);  // write out the file we want to save.
    fos.close(); // close the output stream writer
}
catch(Exception m) {
   System.out.println(m);
}

請提出解決方案。

您使用的鏈接包含文件夾 ID,每個人都應該可以讀取該文件夾。

在這種情況下,您可以使用 Google 驅動器 api 中的 files.list 方法,並在父級中使用“folderid”訪問它,這將返回該文件夾中所有文件的列表。

為此,在我們聊天后,該文件夾需要向觀眾公開,而您的文件夾似乎是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM