Uploading Downloading of large size file to Google

2019-01-15 10:56发布

问题:

I am Creating a small Apps from that user can upload his MS office file to his registered mail ID G_Drive account from android device. and export that file into PDF Format.

for that i taken help form below link :

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

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

i created application, and its working fine for small size(< 1MB) file, but when i am sending large size to the g_Drive than i am getting below errors:

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

i tried for Resumable upload, but the insert method doesn't have parameter to set this. if am setting the upload type to resumable with

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

or

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

than also its generating same errors.

same case with downloading also.....

please send me some solutions...for this...

回答1:

Don't use basic upload for files larger than 5MB.

You need to set the content length of the media content before starting to upload. Otherwise, it's likely that Google endpoint can't recognize the upload's being finished and it waits until the socket is being timed out.

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();

There is actually a complete Android based resumable upload sample on [1].