我正试图使用google-api-services-drive
版本v2-rev93-1.16.0-rc
将大文件插入谷歌的驱动器我已经将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)
有什么想法可以让这个代码工作吗?
由于时间限制,您无法让它在前端工作。唯一可靠的方法(但很痛苦)是使用可恢复上传从后端进行,因为后端/任务队列在处理块时也可能关闭。
"一段时间"恰好意味着1小时吗?在这种情况下,您可能遇到以下错误:
http://code.google.com/p/gdata-issues/issues/detail?id=5124
此问题仅适用于驱动器可恢复媒体上传。请检查此回复。。https://stackoverflow.com/a/30796105/4576135
对我来说,这意味着"你正在进行POST并指定了一个内容长度,但你上传的流不够长,无法匹配该内容长度"(在我的情况下,一个封闭的字节数组流是封闭的,因为以前基本上已经"耗尽"了)。