android - 将大文件上传下载到 Google 云端硬盘时出现错误

标签 android http google-drive-api

我正在创建一个小应用程序,该用户可以从 Android 设备将他的 MS office 文件上传到他注册的邮件 ID G_Drive 帐户。 并将该文件导出为 PDF 格式。

为此我采取了以下链接的帮助形式:

https://developers.google.com/drive/v2/reference/files/insert#examples

https://developers.google.com/drive/manage-downloads#downloading_google_documents

我创建了应用程序,它适用于小尺寸 (< 1MB) 文件,但是当我将大尺寸文件发送到 g_Drive 时,出现以下错误:

java.io.exception Unexpected End of Stream
java.net.SocketTimeoutException: Read timed out

我尝试了可恢复上传,但插入方法没有参数来设置它。 如果我将上传类型设置为可恢复

service.files().insert(body, mediaContent).set("upload type", "resumable");

Insert insertService = service.files().insert(body, mediaContent).setConvert(true);
insertService.getMediaHttpUploader().setDirectUploadEnabled(false);         insertService.getMediaHttpUploader().setChunkSize(MediaHttpUploader.DEFAULT_CHUNK_SIZE);
file = insertService.execute();

它也会产生同样的错误。

同理,下载也......

请给我一些解决方案...为此...

最佳答案

不要对大于 5MB 的文件使用基本上传。

开始上传前需要设置媒体内容的内容长度。否则,Google 端点可能无法识别上传已完成,它会一直等到套接字超时。

InputStreamContent mediaContent = new InputStreamContent("image/jpeg", new   BufferedInputStream(
    new FileInputStream(UPLOAD_FILE)));
mediaContent.setLength(UPLOAD_FILE.length());

Insert insert = drive.files().insert(fileMetadata, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
uploader.setProgressListener(new FileUploadProgressListener());
insert.execute();

[1] 上实际上有一个完整的基于 Android 的可续传上传示例.

关于android - 将大文件上传下载到 Google 云端硬盘时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15970423/

相关文章:

iphone - 如何在 iPhone 上正确处理 HTTP 摘要认证

ruby-on-rails - 带有自定义 header 和正文的 Ruby POST?

node.js - Node : Google Drive API for listing all files using query

ruby - 使用 API 将 Google 幻灯片中的一张幻灯片复制到新的演示文稿中

php - Google API 权限不足列出文件

android - 更新后无法同时运行多个android模拟器

android - 以编程方式截取屏幕截图不会捕获 surfaceVIew 的内容

javascript - 如何向 bitstamp 发出 HTTP 请求?

android - WebView 不在 Android 上播放 swf 文件

android - Cordova 构建 |运行失败 - compileDebugJavaWithJavac