Android上的文件上传暂停和恢复 [英] File upload pause and resume in Android
问题描述
我相信你已经做了正常的上传到服务器。要实现可恢复的上传,您必须将文件拆分为块,并指定要发送到服务器的文件的字节范围。我可以给你一个粗略的想法。
例如:假设你有一个5 MB(百万字节)的文件。您必须按如下方式发送文件。
第一部分:0000000- 9999999 bytes -HTTP 204
2:1000000- 1999999字节-HTTP 204
第3部分:2000000- 2999999字节-HTTP 204
部分4:3000000-3999999字节-HTTP 204
第5部分:4000000-4999999字节HTTP 200
设置以下标题
Content-Length:1000000
内容范围:字节1000000/5000000
您必须指定内容范围并发送相应的字节流数组。除非文件已完成,否则所有成功的块上传都将返回204.如果您在任何时候都上传了中断,则可以轮询服务器并获取上传状态。恢复可以从文件的位置来完成碎片。我们正在把文件分成几块,因为我们不能把所有东西都放在记忆里。可以将整个文件作为一个单独的部分发送,并在确切的字节点恢复为较小的文件。
另一种方法是在本地管理索引。完成每个块后,将索引变量的状态更新为文件。如果要恢复上传,可以从头开始恢复整个块。你不必轮询服务器来获取状态。
成功完成上传后,服务器将返回200状态(即使您将整个文件上传到单个块设置标题)。请参阅Youtube的文档或Pandastream了解更多详情。
https://developers.google.com/youtube/2.0/developers_guide_protocol_resumable_uploads
I have a android application.I want implement the function upload the image/video into server by thread or AsyncTasks.For uploading the image/video,I want implement the pause and resume function.That means it will must be maintain the pause and resume function when it will upload into server.Now i have query is it possible implement this funcation(pause and resume)?If have any idea about this then please help to me ,how i will maintain this.Thanks all.
I believe you have done the normal uploads to the server. To implement a resumable upload you have to split the file into chunks and specify the byte range of file that is being send to server. I can give you a rough idea on this.
Eg: Suppose you have a 5 MB(million bytes) file. You have to send the file as follows.
Part 1: 0000000- 9999999 bytes -HTTP 204
Part 2: 1000000- 1999999 bytes -HTTP 204
Part 3: 2000000- 2999999 bytes -HTTP 204
Part 4: 3000000- 3999999 bytes -HTTP 204
Part 5: 4000000- 4999999 bytes -HTTP 200
Set the following headers
Content-Length: 1000000
Content-Range: bytes 1000000/5000000
You have to specify the content range and send the corresponding byte stream array. Unless a file is complete, all the successful chunk uploads will return a 204. In case you upload breaks at any point, you can poll the server and get the status of upload. Resuming can be done from that point of file to complete the chunk where it broke. We are splitting the file into chunks as we cannot hold the whole thing in memory. It is ok to send the whole file as one single part and resume at the exact byte point for smaller files.
Another option is to manage the index locally. On completion of every chunk, update the status of index variable to a file. If an upload is to be resumed you can resume the whole chunk from beginning. You don't have to poll server to get the status.
After a succesful complete upload, 200 status is returned by server(Even if you upload the whole file in a single chunk with the headers set). Please refer to the Youtube's documentation or Pandastream for more details.
https://developers.google.com/youtube/2.0/developers_guide_protocol_resumable_uploads
这篇关于Android上的文件上传暂停和恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!