我尝试了很多方法来下载Google Drive公共文件但找不到任何答案? 如果有人知道该怎么做,请帮助我。
注意:不使用 Google Drive api
大多数文件(文档、工作表、演示文稿等)都可以通过将 URL 中的 edit 替换为 export?format= 来下载。
https://docs.google.com/document/d/FILE-ID/edit
https://docs.google.com/document/d/FILE-ID/export?format=doc
有很多格式可用(pdf/doc/pptx/xlsx/csv/jpg/等...)
上述解决方案通常适用于 pdf、图像、原生谷歌文件和微软 Office 文件。但是,如果您是 尝试下载其他类型的文件,您可以使用下一个 URL。
https://drive.google.com/uc?export=download&id=FILE-ID
// Tested with a google drive public link. Successfully downloaded.
// Change the suffix of the Google Drive link from /edit to /export?format=csv
// A sample link :
// https://docs.google.com/spreadsheets/d/XYZ/export?format=csv
//
public static boolean downloadWithRedirect(String downloadUrl, String storagePath){
InputStream input = null;
OutputStream output = null;
try {
URL obj = new URL(downloadUrl);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
boolean redirect = false;
// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
if (redirect) {
// get redirect url from "location" header field
String newUrl = conn.getHeaderField("Location");
// get the cookie if need, for login
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
Log.i(TAG, "[downloadWithRedirect] Redirect to URL : " + newUrl);
}
input = new BufferedInputStream(conn.getInputStream(), 1024 * 8);
output = new FileOutputStream(storagePath);
// Ensure output directory exists.
File outputFile = new File(storagePath);
FileUtil.mkDirs(new File(outputFile.getParent()));
Log.i(TAG, "[download] url:" + downloadUrl + ", storagePath:" + storagePath + ", download stream length in bytes:" + conn.getContentLength());
writeToOutputStream(input, output);
return isFileValid(storagePath);
} catch (Exception e) {
Log.e(TAG, "[downloadWithRedirect] Download failed. " + downloadUrl + " " + e.getMessage(), e);
return false;
} finally {
try{if(output != null) output.close();}catch(Exception e){}
try{if(input != null) input.close();}catch(Exception e){}
}
}
private static void writeToOutputStream(InputStream is, OutputStream os) throws IOException {
byte[] buffer = new byte[1024 * 8];
int length;
if (is != null) {
while ((length = is.read(buffer)) > 0x0) {
os.write(buffer, 0x0, length);
}
}
os.flush();
}