Download classes that support multiple file types in Android

Source: Internet
Author: User

  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) {}}}}


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.