Android implements multi threaded breakpoint download method

  • 2020-06-19 11:42:09
  • OfStack

This article gives an example of how Android implements multithreaded breakpoint downloads. Share to everybody for everybody reference. Specific implementation methods are as follows:


package cn.itcast.download; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.RandomAccessFile; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.ProtocolException; 
import java.net.URL; 
import cn.itcast.mutiledownload.StreamTool; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 
public class MutiledownloadActivity extends Activity implements OnClickListener { 
  private ProgressBar pb; 
  private Button bt; 
  private TextView tv; 
  private EditText et; 
  boolean flag = true; 
  boolean stopflag = false; 
  private Handler handler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
      pb.setProgress(total); 
      int max = pb.getMax(); 
      if (total >= (max - 1)) { 
        total = max; 
        flag = false; 
      } 
      int result = total * 100 / max; 
      tv.setText(" The current progress  :" + result + "%"); 
      super.handleMessage(msg); 
    } 
  }; 
  int total = 0; 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    pb = (ProgressBar) this.findViewById(R.id.pb); 
    bt = (Button) this.findViewById(R.id.bt); 
    tv = (TextView) this.findViewById(R.id.tv_process); 
    et = (EditText) this.findViewById(R.id.et); 
    bt.setOnClickListener(this); 
  } 
  @Override 
  public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.bt: 
      //  create 1 Child threads   Regular updates ui 
      if(" Start the download ".equals(bt.getText().toString())){ 
        bt.setText(" suspended "); 
        stopflag = false; // Start the download   
      } 
      else { 
        bt.setText(" Start the download "); 
        stopflag = true; 
      } 
        new Thread() { 
          @Override 
          public void run() { 
            super.run(); 
            while (flag) { 
              try { 
                sleep(1000); 
                //  if total > =  The length of the file  
                Message msg = new Message(); 
                handler.sendMessage(msg); 
              } catch (InterruptedException e) { 
                e.printStackTrace(); 
              } 
            } 
          } 
        }.start(); 
 
        //  Start the download operation  
        String path = et.getText().toString().trim(); 
        if ("".equals(path)) { 
          Toast.makeText(this, " The path cannot be empty ", 1).show(); 
          return; 
        } 
        try { 
          URL url = new URL(path); 
          HttpURLConnection conn = (HttpURLConnection) url 
              .openConnection(); 
          conn.setRequestMethod("GET"); 
          conn.setConnectTimeout(5000); 
          conn.setRequestProperty("User-Agent", 
              "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 
          int code = conn.getResponseCode(); 
          if (code == 200) { 
            int len = conn.getContentLength(); 
            RandomAccessFile file = new RandomAccessFile( 
                "/mnt/sdcard/" + getFilenName(path), "rwd"); 
            // 1. Set the local file size to the server file size 1 to  
            file.setLength(len); 
            //  Sets the maximum value of the progress bar  
            pb.setMax(len); 
            // 2 . Assume that open 3  A thread  
            int threadnumber = 3; 
            int blocksize = len / threadnumber; 
            /** 
             *  thread 1 0~ blocksize  thread 2 1*bolocksize ~ 2*blocksize  thread 3 
             * 2*blocksize ~  The end of the file  
             */ 
            for (int i = 0; i < threadnumber; i++) { 
              int startposition = i * blocksize; 
              int endpositon = (i + 1) * blocksize; 
              if (i == (threadnumber - 1)) { 
                //  The last 1 A thread  
                endpositon = len; 
              } 
              DownLoadTask task = new DownLoadTask(i, path, 
                  startposition, endpositon); 
              task.start(); 
            } 
          } 
        } catch (Exception e) { 
          Toast.makeText(this, " Download exception ", 0).show(); 
          e.printStackTrace(); 
        } 
      break; 
    } 
  } 
  class DownLoadTask extends Thread { 
    int threadid; 
    String filepath; 
    int startposition; 
    int endpositon; 
    public DownLoadTask(int threadid, String filepath, int startposition, 
        int endpositon) { 
      this.threadid = threadid; 
      this.filepath = filepath; 
      this.startposition = startposition; 
      this.endpositon = endpositon; 
    } 
    @Override 
    public void run() { 
      try { 
        File postionfile = new File("/mnt/sdcard/" + threadid + ".txt"); 
        URL url = new URL(filepath); 
        HttpURLConnection conn = (HttpURLConnection) url 
            .openConnection(); 
        System.out.println(" thread " + threadid + " Is downloading  " + " The starting position  : " 
            + startposition + " End position  " + endpositon); 
        if (postionfile.exists()) { 
          FileInputStream fis = new FileInputStream(postionfile); 
          byte[] result = StreamTool.getBytes(fis); 
          String str = new String(result); 
          if (!"".equals(str)) { 
            int newstartposition = Integer.parseInt(str); 
            if (newstartposition > startposition) { 
              startposition = newstartposition; 
            } 
          } 
        } 
        // "Range", "bytes=2097152-4194303") 
        conn.setRequestProperty("Range", "bytes=" + startposition + "-" 
            + endpositon); 
        conn.setRequestMethod("GET"); 
        conn.setConnectTimeout(5000); 
        conn.setRequestProperty("User-Agent", 
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 
        InputStream is = conn.getInputStream(); 
        RandomAccessFile file = new RandomAccessFile("/mnt/sdcard/" 
            + getFilenName(filepath), "rwd"); 
        //  Set up the   Where data is written from in the file  
        file.seek(startposition); 
        byte[] buffer = new byte[1024]; 
        int len = 0; 
        //  Represents the location of the server data currently read  , At the same time the value is the location of the file that has been stored  
        int currentPostion = startposition; 
        //  create 1 File object  , Record the current download location of a file  
        while ((len = is.read(buffer)) != -1) { 
          if (stopflag) { 
            return; 
          } 
          file.write(buffer, 0, len); 
          synchronized (MutiledownloadActivity.this) { 
            total += len; 
          } 
          currentPostion += len; 
          //  Need to put currentPostion  Information is persisted to the storage device  
          String position = currentPostion + ""; 
          FileOutputStream fos = new FileOutputStream(postionfile); 
          fos.write(position.getBytes()); 
          fos.flush(); 
          fos.close(); 
        } 
        file.close(); 
        System.out.println(" thread " + threadid + " The download "); 
        //  When the thread has finished downloading   Delete the file  
        if (postionfile.exists()) { 
          postionfile.delete(); 
        } 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
      super.run(); 
    } 
  } 
  public String getFilenName(String path) { 
    int start = path.lastIndexOf("/") + 1; 
    return path.substring(start, path.length()); 
  } 
}

Hopefully, this article has been helpful in your Android programming.


Related articles: