Upload large size file (approx 2MB) to server
I am developing an Android mobile application which has the functionality of uploading files to the server sequentially.
I wanted to write the client server communication code keeping a criteria in mind.
Since the size of the file to be uploaded from the mobile device will be around 2MB , we need to send the file in multiple parts(chunks of 开发者_运维百科data) to the server . Before sending the file to the server , I need to determine the signal strength at runtime. e.g: In a Wi-Fi environment , where the signal strength will be more , the no of chunks will be less & the chunk size will be more when compared to using a 2G/3G connection(using SIM Card) where the no of chunks will be more & chunk size will be less.
Any suggestions/sample code to implement the same will be helpful to me. Also , is there any Android or third party API which can take care of the requirement.
Thanks in advance.
CB
You can take a look at PhoneStateListener to determine signal strength. Then just set a chunkSize based on it.
I had to write an app where I was uploading/downloading large files (upwards of 10-20MB) with httppost. Downloading is easy because you can use the http "range" header. I couldn't figure out a way to do this with uploading though.
The best method I could think of is to open your file with RandomAccessFile, read one chunk from your file and write it to a temporary file, and then upload that temp file. Proceed through the entire file, creating a temp file for each chunk. Keep track of which chunkNum you are on in a preference. If it succeeds, you set chunkNum back to 0.
If the connection fails in the middle of the upload, the next time you try to upload it, you first check your chunkNum preference to see if it is greater than 0. RandomAccessFile's seek() method to move the file pointer to the appropriate chunk and resume.
Unfortunately, this method adds latency in creating the temp files, but I couldn't think of a better solution.
精彩评论