我正在尋找一種上傳谷歌驅動器上下載鏈接文件的方法。如何上傳帶有鏈接到Google Drive REST API v2的文件
我在文檔中沒有找到解釋如何去做的東西。
編輯:
我發現了兩個備選方案:
- 首先是輸出來自環路
- 第二的createdFile是第一次迭代恢復該文件的id然後進行更新。
第一個解決方案似乎是最快的。例如,當我從一個雲到另一個雲進行文件複製/粘貼時,使用固體資源管理器會快得多。 必須有一個更好的解決辦法?`
@Override
protected String doInBackground(String... sUrl) {
File body = new File();
body.setTitle("some name");
String fileId = null;
File createdFile;
InputStream input = null;
ByteArrayOutputStream bos = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// download the file
input = connection.getInputStream();
bos = new ByteArrayOutputStream();
byte data[] = new byte[4096];
total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
bos.write(data, 0, count);
//option 2
if (fileId == null){
createdFile = mService.files().insert(body, new ByteArrayContent("", bos.toByteArray())).execute();
fileId = createdFile.getId();
}else{
createdFile = mService.files().update(fileId, createdFile, new ByteArrayContent("", bos.toByteArray())).execute();
}
}
//option 1
createdFile = mService.files().insert(body, new ByteArrayContent("", bos.toByteArray())).execute();
} catch (Exception e) {
return e.toString();
} finally {
try {
if (bos != null)
bos.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
..............
你能告訴我一個解決方案嗎?
我找到了兩個替代方案: - 第一個是從循環 輸出createdFile - 第二個是第一次迭代恢復文件的id然後進行更新。 第一個解決方案似乎是最快的。例如,當我從一個雲到另一個雲進行文件複製/粘貼時,使用固體資源管理器會快得多。 必須有更好的解決方案嗎? – Aristide13