将大文件插入Google驱动器时,IOException“写入的数据不足”

时间:2013-08-24 17:38:27

标签: java io google-api google-drive-api google-api-java-client

我正在尝试使用google-api-services-drivev2-rev93-1.16.0-rc将大文件插入到Google的驱动器中 我已将setChunkSize()设置为最低限度,以便更频繁地通知我自己的ProgressListener。以下代码用于插入文件:

File body = new File();
body.setTitle(filetobeuploaded.getName());
body.setMimeType("application/zip");
body.setFileSize(filetobeuploaded.length());

InputStreamContent mediaContent =
        new InputStreamContent("application/zip",
        new BufferedInputStream(new FileInputStream(filetobeuploaded)));
mediaContent.setLength(filetobeuploaded.length());

Insert insert = drive.files().insert(body, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
uploader.setProgressListener(new CustomProgressListener(filetobeuploaded));
insert.execute();

'一段时间'(有时200 MB有时300 MB)我得到了IOException

Exception in thread "main" java.io.IOException: insufficient data written
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:3213)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:81)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:960)
at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequest(MediaHttpUploader.java:482)
at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:390)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:418)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)

有关如何使此代码正常工作的任何想法?

4 个答案:

答案 0 :(得分:0)

由于时间限制,你无法从前端开始工作。唯一可靠的方法(但很痛苦)是使用可恢复上传从后端执行此操作,因为后端/任务队列也可能在处理块时关闭。

答案 1 :(得分:0)

'一段时间'是否意味着1小时?在这种情况下,您可能会遇到以下错误:

http://code.google.com/p/gdata-issues/issues/detail?id=5124

答案 2 :(得分:0)

此问题仅适用于Drive Resumable Media上传。检查这个回复.. https://stackoverflow.com/a/30796105/4576135

答案 3 :(得分:0)

对我而言,这意味着“您正在进行POST并指定内容长度,但是您上传的流不足以匹配该内容长度”(在我的情况下,封闭的bytearray流因为之前使用过而关闭)基本上“已经筋疲力尽”了,就像它一样。)