【发布时间】:2012-03-30 10:37:18
【问题描述】:
最近,我尝试使用 google-api-java-client 库来访问 google 文档。我可以使用这个库来获取列表、创建新文档并成功更新。但是,当我想从 Google Document 下载一个大文件 (> 10mb) 时,会导致 OutOfMemoryError。
03-30 17:57:10.650: E/AndroidRuntime(11022): java.lang.OutOfMemoryError
03-30 17:57:10.650: E/AndroidRuntime(11022): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
03-30 17:57:10.650: E/AndroidRuntime(11022): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
03-30 17:57:10.650: E/AndroidRuntime(11022): at com.google.api.client.http.AbstractInputStreamContent.copy(AbstractInputStreamContent.java:213)
03-30 17:57:10.650: E/AndroidRuntime(11022): at com.google.api.client.http.AbstractInputStreamContent.copy(AbstractInputStreamContent.java:179)
03-30 17:57:10.650: E/AndroidRuntime(11022): at com.google.api.client.http.HttpResponse.getContent(HttpResponse.java:384)
这是我的代码:
try {
...
HttpRequest request = getRequestFactory().buildGetRequest(link);
HttpResponse response = request.execute();
InputStream is = response.getContent();
...
} catch (IOException e) {
e.printStackTrace();
}
响应码为200,可以成功处理小文件。 有什么方法可以使用 google-api-java-client 下载大文件吗?
【问题讨论】:
-
我认为您在代码中现在三个点所在的位置是相关部分。如果文件太大,您必须将其流式传输到文件系统,因为您无法将其保存在内存中。
-
我发现问题是api函数response.getContent(),它使用ByteArrayOutputStream来保存文件数据。所以当文件过大时,会导致OutOfMemoryError。
标签: java google-api google-api-java-client