String directoryname = Environment.getexternalstoragedirectory (). toString () + "/filename";////file save path// Incoming parameters: Context object, file save path; Downloadtask downloadtask = new Downloadtask (this, Mdownloadurl, directoryname); New Thread (Downloadtask). Start ();///////start thread for download
Download class public class Downloadtask implements Runnable {private long mdownloadedsize = 0; Private long mtotalsize;private int mdownloadpercent; Private String Mlocalpath; Private String Murl; Private Context Mcontext; Public Downloadtask (context context, string URL, string localPath) {This.mlocalpath = LocalPath; This.murl = URL; This.mcontext = context; } @Override public void Run () {download (); };////Download Method protected Boolean download () {File File = new file (Mlocalpath); if (file.exists ()) {mdownloadedsize = File.length (); } else {mdownloadedsize = 0; } log.d (TAG, "Murl," + Murl + "downloadedsize," + mdownloadedsize); HttpURLConnection httpconnection = null; URL url = null; try {url = new URL (mupgradeurl); Httpconnection = (httpurlconnection) url.openconnection (); Mtotalsize = Httpconnection.getcontentlength (); LOG.D (TAG, "totalsize," + mtotalsize); if (mdownloadedsize = = mtotalsize) {////////has been downloaded to local return true; } else if (Mdownloadedsize > Mtotalsize) {if (!file.delete ()) {return false; }} httpconnection.disconnect (); } catch (Exception e) {e.printstacktrace (); return false; } finally {try {if (httpconnection! = null) {httpconnection.disconnect (); }} catch (Exception e) {}} InputStream instream = null; Randomaccessfile randomaccessfile = null; try {httpconnection = (httpurlconnection) url.openconnection (); Httpconnection.setrequestproperty ("Accept", "image/gif," + "image/jpeg," + "image/pjpeg," + "image/p JPEG, "+" Application/x-shockwave-flash, " + "Application/xaml+xml," + "application/vnd.ms-xpsdocument," + "application/x-ms-xbap," + "a Pplication/x-ms-application, "+" application/vnd.ms-excel, "+" Application/vnd.ms-powerpoint, " + "Application/msword," + "*/*"); Httpconnection.setrequestproperty ("Accept-language", "ZH-CN"); Httpconnection.setrequestproperty ("Referer", Mupgradeurl); Httpconnection.setrequestproperty ("Charset", "UTF-8"); Httpconnection.setrequestproperty ("Range", "bytes=" + mdownloadedsize + "-"); Httpconnection.setrequestproperty ("Connection", "keep-alive"); Instream = Httpconnection.getinputstream (); File SaveFile = new file (Mlocalpath); Randomaccessfile = new Randomaccessfile (SaveFile, "RWD"); Randomaccessfile.seek (mdownloadedsize); int offset = 0; int count = 0; int perunit = (int) mtotalsize/1024/100; byte[] buffer = new byte[1024]; while (offset = instream.read (buffer, 0, 1024x768)! =-1) {randomaccessfile.write (buffer, 0, offset); count++; if (count = = Perunit && mdownloadedsize < mtotalsize) {mdownloadpercent = (int) (Mdownloade Dsize * 100/mtotalsize); Download hundred mdownloadpercent count = 0; } mdownloadedsize + = offset; } if (mdownloadedsize = = mtotalsize) {/////////Download complete} log.d (TAG, "download Finished. "); return true; } catch (Exception e) {e.printstacktrace (); return false; } finally {try {if (instream! = null) {instream.close (); } if (httpconnection! = null) {httpconnection.disconnect (); } if (randomaccessfile! = null) {randomaccessfile.close (); }} catch (Exception e) {}}}}