安卓系統下的多線程斷點下載實現

來源:互聯網
上載者:User

標籤:remove   主線程   安卓   []   static   ast   exists   處理   adl   

近期研究多線程下載,寫了個demo。整理下來,或許會對別人有協助。
多線程下載的話一般開啟兩到三個線程吧。假設線程太多的話時間會浪費線上程的切換上,倒是浪費了大把的時間。線程多了也不是一件好事。
原理的話看我的還有一篇博文,其實是將代碼移植到了安卓系統上。java實現的多線程下載demo

public class MainActivity extends Activity {    protected static final int DOWNLOAD_ERROR = 1;    private static final int THREAD_ERROR = 2;    public static final int DWONLOAD_FINISH = 3;    private EditText et_path;    private EditText et_count;    /**     * 存放進度條的布局     */    private LinearLayout ll_container;    /**     * 進度條的集合     */    private List<ProgressBar> pbs;    /**     * android下的訊息處理器。在主線程建立。才幹夠更新ui     */    private Handler handler = new Handler(){        public void handleMessage(Message msg) {            switch (msg.what) {            case DOWNLOAD_ERROR:                Toast.makeText(getApplicationContext(), "下載失敗", 0).show();                break;            case THREAD_ERROR:                Toast.makeText(getApplicationContext(), "下載失敗,請重試", 0).show();                break;            case DWONLOAD_FINISH:                Toast.makeText(getApplicationContext(), "完成下載", 0).show();                break;            }        };    };    /**     * 線程的數量     */    private int threadCount = 3;    /**     * 每一個下載區塊的大小     */    private long blocksize;    /**     * 正在執行的線程的數量     */    private  int runningThreadCount;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        et_path = (EditText) findViewById(R.id.et_addr);        et_count = (EditText) findViewById(R.id.et_num);        ll_container = (LinearLayout) findViewById(R.id.ll_pb);    }    public void download(View view){        //下載檔案的路徑        final String path = et_path.getText().toString().trim();        if(TextUtils.isEmpty(path)){            Toast.makeText(this, "對不起下載路徑不可為空", 0).show();            return;        }        String count = et_count.getText().toString().trim();        if(TextUtils.isEmpty(path)){            Toast.makeText(this, "對不起,線程數量不可為空", 0).show();            return;        }        threadCount = Integer.parseInt(count);        //清空掉舊的進度條        ll_container.removeAllViews();        //在介面裡面加入count個進度條        pbs = new ArrayList<ProgressBar>();        for(int j=0;j<threadCount;j++){            ProgressBar pb = (ProgressBar) View.inflate(this, R.layout.pb, null);            ll_container.addView(pb);            pbs.add(pb);        }        Toast.makeText(this, "開始下載", 0).show();        new Thread(){            public void run() {                try {                    URL url = new URL(path);                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();                    conn.setRequestMethod("GET");                    conn.setConnectTimeout(5000);                    int code = conn.getResponseCode();                    if (code == 200) {                        long size = conn.getContentLength();// 得到服務端返回的檔案的大小                        System.out.println("server檔案的大小:" + size);                        blocksize = size / threadCount;                        // 1.首先在本地建立一個大小跟server一模一樣的空白檔案。                        File file = new File(Environment.getExternalStorageDirectory(),getFileName(path));                        RandomAccessFile raf = new RandomAccessFile(file, "rw");                        raf.setLength(size);                        // 2.開啟若干個子線程分別去下載相應的資源。

runningThreadCount = threadCount; for (int i = 1; i <= threadCount; i++) { long startIndex = (i - 1) * blocksize; long endIndex = i * blocksize - 1; if (i == threadCount) { // 最後一個線程 endIndex = size - 1; } System.out.println("開啟線程:" + i + "下載的位置:" + startIndex + "~" + endIndex); int threadSize = (int) (endIndex - startIndex); pbs.get(i-1).setMax(threadSize); new DownloadThread(path, i, startIndex, endIndex).start(); } } conn.disconnect(); } catch (Exception e) { e.printStackTrace(); Message msg = Message.obtain(); msg.what = DOWNLOAD_ERROR; handler.sendMessage(msg); } }; }.start(); } private class DownloadThread extends Thread { private int threadId; private long startIndex; private long endIndex; private String path; public DownloadThread(String path, int threadId, long startIndex, long endIndex) { this.path = path; this.threadId = threadId; this.startIndex = startIndex; this.endIndex = endIndex; } @Override public void run() { try { // 當前線程下載的總大小 int total = 0; File positionFile = new File(Environment.getExternalStorageDirectory(),getFileName(path)+threadId + ".txt"); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); // 接著從上一次的位置繼續下載資料 if (positionFile.exists() && positionFile.length() > 0) {// 推斷是否有記錄 FileInputStream fis = new FileInputStream(positionFile); BufferedReader br = new BufferedReader( new InputStreamReader(fis)); // 擷取當前線程上次下載的總大小是多少 String lasttotalstr = br.readLine(); int lastTotal = Integer.valueOf(lasttotalstr); System.out.println("上次線程" + threadId + "下載的總大小:" + lastTotal); startIndex += lastTotal; total += lastTotal;// 加上上次下載的總大小。 fis.close(); //存資料庫。 //_id path threadid total } conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); System.out.println("code=" + code); InputStream is = conn.getInputStream(); File file = new File(Environment.getExternalStorageDirectory(),getFileName(path)); RandomAccessFile raf = new RandomAccessFile(file, "rw"); // 指定檔案開始寫的位置。

raf.seek(startIndex); System.out.println("第" + threadId + "個線程:寫檔案的開始位置:" + String.valueOf(startIndex)); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { RandomAccessFile rf = new RandomAccessFile(positionFile, "rwd"); raf.write(buffer, 0, len); total += len; rf.write(String.valueOf(total).getBytes()); rf.close(); pbs.get(threadId-1).setProgress(total); } is.close(); raf.close(); } catch (Exception e) { e.printStackTrace(); Message msg = Message.obtain(); msg.what = THREAD_ERROR; handler.sendMessage(msg); } finally { // 僅僅有全部的線程都完成下載後 才幹夠刪除記錄檔案。 synchronized (MainActivity.class) { System.out.println("線程" + threadId + "完成下載了"); runningThreadCount--; if (runningThreadCount < 1) { System.out.println("全部的線程都工作完成了。

刪除暫時記錄的檔案"); for (int i = 1; i <= threadCount; i++) { File f = new File(Environment.getExternalStorageDirectory(),getFileName(path)+ i + ".txt"); System.out.println(f.delete()); } Message msg = Message.obtain(); msg.what = DWONLOAD_FINISH; handler.sendMessage(msg); } } } } } private String getFileName(String path){ int start = path.lastIndexOf("/")+1; return path.substring(start); }}


安卓系統下須要加上訪問網路的許可權和訪問本地記憶卡的許可權。例如以:

原始碼已經上傳到了CSDN: http://download.csdn.net/detail/rootusers/8508137

安卓系統下的多線程斷點下載實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.