我正在寻找一种上传谷歌驱动器上下载链接文件的方法。如何上传带有链接到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