安卓開發:多線程斷點續傳下載文件

来源:https://www.cnblogs.com/xuyiqing/archive/2018/04/19/8885561.html
-Advertisement-
Play Games

一個簡單的界面: item.xml: 代碼: 開許可權: ...


一個簡單的界面:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_path"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入下載路徑(網址):" />

    <EditText
        android:id="@+id/et_threadCount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入開啟線程數量(建議不超過5):" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="開始下載" />

    
    <LinearLayout 
        android:id="@+id/ll_pb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        ></LinearLayout>
</LinearLayout>

 

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

 

代碼:

package org.dreamtech.download;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private EditText et_path;
    private EditText et_threadCount;
    private LinearLayout ll_pb_layout;
    private static int runningThread;
    private String path;
    private int threadCount;
    private List<ProgressBar> pbLists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_path = (EditText) findViewById(R.id.et_path);
        et_threadCount = (EditText) findViewById(R.id.et_threadCount);
        ll_pb_layout = (LinearLayout) findViewById(R.id.ll_pb);

        pbLists = new ArrayList<ProgressBar>();

    }

    public void click(View v) {
        path = et_path.getText().toString().trim();
        threadCount = Integer.parseInt(et_threadCount.getText().toString()
                .trim());
        ll_pb_layout.removeAllViews();
        pbLists.clear();
        for (int i = 0; i < threadCount; i++) {
            ProgressBar pbView = (ProgressBar) View.inflate(
                    getApplicationContext(), R.layout.item, null);

            pbLists.add(pbView);

            ll_pb_layout.addView(pbView);
        }
        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) {

                        int length = conn.getContentLength();

                        runningThread = threadCount;

                        System.out.println("length:" + length);

                        RandomAccessFile rafAccessFile = new RandomAccessFile(
                                getFilename(path), "rw");
                        rafAccessFile.setLength(length);

                        int blockSize = length / threadCount;

                        for (int i = 0; i < threadCount; i++) {
                            int startIndex = i * blockSize;
                            int endIndex = (i + 1) * blockSize - 1;

                            if (i == threadCount - 1) {

                                endIndex = length - 1;

                            }

                            DownLoadThread downLoadThread = new DownLoadThread(
                                    startIndex, endIndex, i);
                            downLoadThread.start();

                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private class DownLoadThread extends Thread {

        private int startIndex;
        private int endIndex;
        private int threadId;
        private int PbMaxSize;
        private int pblastPosition;

        public DownLoadThread(int startIndex, int endIndex, int threadId) {
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.threadId = threadId;
        }

        @Override
        public void run() {

            try {
                PbMaxSize = endIndex - startIndex;

                URL url = new URL(path);

                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();

                conn.setRequestMethod("GET");

                conn.setConnectTimeout(5000);

                File file = new File(Environment.getExternalStorageDirectory()
                        .getPath()
                        + "/"
                        + getFilename(path)
                        + threadId
                        + ".txt");
                if (file.exists() && file.length() > 0) {
                    FileInputStream fis = new FileInputStream(file);
                    BufferedReader bufr = new BufferedReader(
                            new InputStreamReader(fis));
                    String lastPositionn = bufr.readLine();
                    int lastPosition = Integer.parseInt(lastPositionn);

                    pblastPosition = lastPosition - startIndex;

                    startIndex = lastPosition + 1;

                    fis.close();
                }

                conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
                        + endIndex);

                int code = conn.getResponseCode();

                if (code == 206) {

                    RandomAccessFile raf = new RandomAccessFile(
                            getFilename(path), "rw");

                    raf.seek(startIndex);

                    InputStream in = conn.getInputStream();

                    int len = -1;
                    byte[] buffer = new byte[1024 * 1024];

                    int total = 0;

                    while ((len = in.read(buffer)) != -1) {
                        raf.write(buffer, 0, len);

                        total += len;
                        int currentThreadPosition = startIndex + total;

                        RandomAccessFile raff = new RandomAccessFile(
                                getFilename(path) + threadId + ".txt", "rwd");
                        raff.write(String.valueOf(currentThreadPosition)
                                .getBytes());
                        raff.close();

                        pbLists.get(threadId).setMax(PbMaxSize);
                        pbLists.get(threadId).setProgress(
                                pblastPosition + total);

                    }
                    raf.close();

                    synchronized (DownLoadThread.class) {
                        runningThread--;
                        if (runningThread == 0) {
                            for (int i = 0; i < threadCount; i++) {
                                File delteFile = new File(getFilename(path) + i
                                        + ".txt");
                                delteFile.delete();
                            }

                        }
                    }

                }

            } catch (Exception e) {
            }

        }
    }

    public String getFilename(String path) {

        int start = path.lastIndexOf("/") + 1;
        String subString = path.substring(start);
        String filename = Environment.getExternalStorageDirectory().getPath()+"/"+subString;
        
        return filename;
    }
}

 

 

開許可權:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文為mariadb官方手冊:INSERT ON DUPLICATE KEY UPDATE的譯文。 原文:https://mariadb.com/kb/en/insert-on-duplicate-key-update/ 我提交到MariaDB官方手冊的譯文:https://mariadb.com/ ...
  • 隨著Linux 7 版本的普及,但Oracle資料庫主流版本仍是11gR2,11.2.0.4 是生產安裝首選。由於11.2.0.4對Linux 7 的支持不很完美,在Linux 7 上安裝會遇到幾處問題,以此記錄下來。 https://docs.oracle.com/cd/E11882_01/rel ...
  • 16 Managing Undo 官網:http://docs.oracle.com/cd/E11882_01/server.112/e25494/undo.htm#ADMIN013 從Oracle11g開始,在預設安裝中oracle會自動管理undo, 典型安裝中不需要DBA介入配置,然而,如果選 ...
  • 並行複製從庫更新的記錄不存在實際卻存在 背景 開了並行複製的半同步從庫SQL 線程報1032錯誤,非同步複製從庫沒有報錯,偶爾會出現這種 分析 版本mysql 5.7.16 mysql show variables like '%slave_para%'; + + + | Variable_name ...
  • 並行複製從庫發生自動重啟分析 背景 半同步複製從庫在晚上凌晨2點半發生自動重啟,另一個非同步複製從庫在第二天凌晨3點也發生了自動重啟。 分析 版本mysql 5.7.16 mysql show variables like '%slave_para%'; + + + | Variable_name | ...
  • Getting Started Getting Started. 1 1. Introduction. 1 2.Quick Start-Strandalone HBase. 1 2.1 JDK版本選擇... 1 2.2 Get Started With HBase. 1 2.3 偽分散式本地安裝.. ...
  • 正則表達式通常稱為regexes,是文本處理中模式匹配的一個標準,也是處理字元串的一個強有力的工具。使用正則表達式時,需要指定一個字元串作為模式串去檢索目標字元串。你可以使用正則表達式來查找字元串中匹配該正則表達式表示的模式的子串,也可以進行文本替換或者從目標文本中提取子串。 參考資料《iOS編程指 ...
  • 一個曲線 圖例: 多個曲線 圖例: ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...