Android resumable upload code

Source: Internet
Author: User

/** <Br/> * resumable upload <br/> * @ Param URL <br/> */<br/> private void duandianxuchuan (final string URL) {<br/> try {<br/> runnable r = new runnable () {<br/> @ override <br/> Public void run () {<br/> getremotefile (URL); <br/>}< br/>}; <br/> New thread (R ). start (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>/** <br/> * download a remote file <br/> * @ Param urlstring <br/> */< br/> private void getremotef Ile (string urlstring) {<br/> try {<br/> file; <br/> If (mcursize = 0) {<br/> file = file. createtempfile (filena, fileex); <br/> currenttempfilepath = file. getabsolutepath (); <br/> log. E ("Andy", file. getname () + "has exists. "); <br/>}else {<br/> file = new file (currenttempfilepath); <br/> fileinputstream FD = new fileinputstream (File ); <br/> mcursize = Fi. available (); <br/> log. E ("Andy", file. getname () + "Has" + mcursize + "bytes. "); <br/>}< br/> URL url = new URL (urlstring); <br/> httpurlconnection httpconnection = (httpurlconnection) URL <br/>. openconnection (); <br/> httpconnection. setrequestproperty ("User-Agent", "netfox"); <br/> string sproperty = "bytes =" + mcursize + "-"; <br/> httpconnection. setrequestproperty ("range", sproperty); <br/> inputstream is = httpconnection. getinputstream (); <br /> Log. E ("Andy", "mtempfilename =" + mtempfilename); <br/>/* write the file to the temporary storage disk */<br/> fileoutputstream Fos = new fileoutputstream (file, true); <br/> byte Buf [] = new byte [512]; <br/> DO <br/>{< br/> int numread = is. read (BUF); <br/> If (numread <= 0) <br/>{< br/> mhandler. sendemptymessagedelayed (this. download_finish, 0); <br/> break; <br/>}< br/> mcursize + = numread; <br/> mhandler. sendemptymessagedelayed (this. updat E_download_progress, 0); <br/> log. E ("Andy", "current size =" + mcursize); <br/> FOS. write (BUF, 0, numread); <br/>}while (mcontinue ); <br/>/* open the file for installation */<br/> If (mcursize = msize) {<br/> mcursize = 0; <br/> openfile (File); <br/>}< br/> try <br/>{< br/> is. close (); <br/>}< br/> catch (exception ex) <br/>{< br/> log. E ("Andy", "error:" + ex. getmessage (), Ex); <br/>}< br/>} catch (exception e) {<br/> E. printsta Cktrace (); <br/>}< br/>/** <br/> * obtain the file length <br/> * @ Param urlstring <br/> * @ return <br/> */<br/> Public long getfilesize (string urlstring) {<br/> int nfilelength =-1; <br/> try {<br/> URL url = new URL (urlstring); <br/> httpurlconnection httpconnection = (httpurlconnection) URL <br/>. openconnection (); <br/> httpconnection. setrequestproperty ("User-Agent", "netfox"); <br/> int responsecode = httpcon Nection. getresponsecode (); <br/> If (responsecode> = 400) {<br/> return-2; //-2 represent access is error <br/>}< br/> string sheader; <br/> for (INT I = 1; I ++) {<br/> sheader = httpconnection. getheaderfieldkey (I); <br/> If (sheader! = NULL) {<br/> If (sheader. equals ("Content-Length") {<br/> nfilelength = integer. parseint (httpconnection <br/>. getheaderfield (sheader); <br/> break; <br/>}< br/>} else <br/> break; <br/>}< br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return nfilelength; <br/>}< br/>/** <br/> * calculate the percentage <br/> * @ Param P1 <br/> * @ Param P2 <br/> * @ return <br/> */<br/> Public static string percent (double P1, double P2) {<br/> string STR; <br/> double P3 = p1/P2; <br/> numberformat NF = numberformat. getpercentinstance (); <br/> NF. setminimumfractiondigits (2); <br/> STR = NF. format (P3); <br/> log. E ("Andy", "percentage =" + Str); <br/> return STR; <br/>} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.