package com.itheima.multithreaddownload; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import javax.print.attribute.standard.Finishings; public class MultiDownload { static int ThreadCount = 3; static int finishedThread = 0; //确定下载地址 static String path = "http://192.168.13.13:8080/QQPlayer.exe"; public static void main(String[] args) { //发送get请求,请求这个地址的资源 try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); if(conn.getResponseCode() == 200){ //拿到所请求资源文件的长度 int length = conn.getContentLength();//文件大小 File file = new File("QQPlayer.exe"); //生成和真实文件一样大的临时文件占用硬盘大小,文件名和源文件一样,RandomAccessFile可以很轻易实现每个下载的位置不一样,FileOututstream不能做到,所以用随机文件类RandomAccessFile来下载文件 RandomAccessFile raf = new RandomAccessFile(file, "rwd");//rwd可读写方式并且直接写入硬盘不通过缓冲区,硬盘缓冲区和内存一样,断电就没有了 //(硬盘有缓冲区(闪存),缓冲区满了之后在写入硬盘减少硬盘读写次数,机械硬盘很容易摔坏导致磁头错位就不能用了固态硬盘就不容易坏) //rwd支持断点续传,这次下载300字节如果没有写入硬盘在缓冲区有30字节,那么下次从301字节开始下载就会丢失30字节。 //设置临时文件的大小 raf.setLength(length); raf.close(); //计算出每个线程应该下载多少字节 int size = length / ThreadCount; for (int i = 0; i < ThreadCount; i++) { //计算线程下载的开始位置和结束位置 int startIndex = i * size; int endIndex = (i + 1) * size - 1; //如果是最后一个线程,那么结束位置写最后位置 if(i == ThreadCount - 1){ endIndex = length - 1; } // System.out.println("线程" + i + "的下载区间是:" + startIndex + "---" + endIndex); new DownLoadThread(startIndex, endIndex, i).start();//开启size个子线程下载 } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class DownLoadThread extends Thread{ int startIndex; int endIndex; int threadId; public DownLoadThread(int startIndex, int endIndex, int threadId) { super(); this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; } @Override public void run() { //再次发送http请求,下载原文件 try { File progressFile = new File(threadId + ".txt"); //判断进度临时文件是否存在,存在说明不是第一次下载 if(progressFile.exists()){ FileInputStream fis = new FileInputStream(progressFile); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); //每个线程的断点续传:从进度临时文件中读取出上一次下载的总进度一行total字符串,然后与原本的开始位置相加,得到新的开始位置 startIndex += Integer.parseInt(br.readLine()); fis.close(); } System.out.println("线程" + threadId + "的下载区间是:" + startIndex + "---" + endIndex); HttpURLConnection conn; URL url = new URL(MultiDownload.path); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);//设置不请求所有数据只请求区间的数据 //请求部分数据,相应码是206 if(conn.getResponseCode() == 206){ InputStream is = conn.getInputStream();//此时的流不是整个数据,是startIndex到endIndex的数据 byte[] b = new byte[1024]; int len = 0; int total = 0; //拿到临时文件的输出流,同步写入临时文件 File file = new File("QQPlayer.exe"); RandomAccessFile raf = new RandomAccessFile(file, "rwd");//相当于输出流,直接把数据输出到文件之中。 //把文件的写入位置移动至startIndex raf.seek(startIndex); while((len = is.read(b)) != -1){ raf.write(b, 0, len);//每次读取流里数据之后,同步把数据写入临时文件 total += len; // System.out.println("线程" + threadId + "下载了" + total); //生成一个专门用来记录下载进度的临时text文件,用于断点续传(RandomAccessFile是没有缓存的文件输出流,下载就写入硬盘), RandomAccessFile progressRaf = new RandomAccessFile(progressFile, "rwd"); //每次读取流里数据之后,同步把当前线程下载的总进度total字符串写入进度临时文件中 progressRaf.write((total + "").getBytes()); progressRaf.close(); } raf.close(); MultiDownload.finishedThread++; synchronized (MultiDownload.path) {//用静态变量同步,因为静态变量是唯一的,否则有可能3个线程都进去了删了文件9次 if(MultiDownload.finishedThread == MultiDownload.ThreadCount){ //要3个线程都完成了才把进度临时文件删除,否则如果第一个下载完了则第一个的进度文件删除了, //第二个没有下完,那么第二个人开始的时候发现第一个进度文件不存在就会重新创建后重新下载第一个。 for (int i = 0; i < MultiDownload.ThreadCount; i++) { File f = new File(i + ".txt"); f.delete(); } MultiDownload.finishedThread = 0; } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
手机版的多线程下载断电续传:
package com.itheima.mobilemultidownload; 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 android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { static int ThreadCount = 3; static int finishedThread = 0; int currentProgress; String fileName = "QQPlayer.exe"; //确定下载地址 String path = "http://192.168.13.13:8080/" + fileName; private ProgressBar pb; TextView tv; Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { //把变量改成long,在long下运算 tv.setText((long)pb.getProgress() * 100 / pb.getMax() + "%");//long是防止整型超出范围 } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pb = (ProgressBar) findViewById(R.id.pb); tv = (TextView) findViewById(R.id.tv); } public void click(View v){ Thread t = new Thread(){ @Override public void run() { //发送get请求,请求这个地址的资源 try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); if(conn.getResponseCode() == 200){ //拿到所请求资源文件的长度 int length = conn.getContentLength(); //设置进度条的最大值就是原文件的总长度 pb.setMax(length); File file = new File(Environment.getExternalStorageDirectory(), fileName); //生成临时文件 RandomAccessFile raf = new RandomAccessFile(file, "rwd"); //设置临时文件的大小 raf.setLength(length); raf.close(); //计算出每个线程应该下载多少字节 int size = length / ThreadCount; for (int i = 0; i < ThreadCount; i++) { //计算线程下载的开始位置和结束位置 int startIndex = i * size; int endIndex = (i + 1) * size - 1; //如果是最后一个线程,那么结束位置写死 if(i == ThreadCount - 1){ endIndex = length - 1; } // System.out.println("线程" + i + "的下载区间是:" + startIndex + "---" + endIndex); new DownLoadThread(startIndex, endIndex, i).start(); } } } catch (Exception e) { e.printStackTrace(); } } }; t.start(); } class DownLoadThread extends Thread{ int startIndex; int endIndex; int threadId; public DownLoadThread(int startIndex, int endIndex, int threadId) { super(); this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; } @Override public void run() { //再次发送http请求,下载原文件 try { File progressFile = new File(Environment.getExternalStorageDirectory(), threadId + ".txt"); //判断进度临时文件是否存在 if(progressFile.exists()){//断点续传,获取上次的总进度。 FileInputStream fis = new FileInputStream(progressFile); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); //从进度临时文件中读取出上一次下载的总进度,然后与原本的开始位置相加,得到新的开始位置 int lastProgress = Integer.parseInt(br.readLine()); startIndex += lastProgress; //把上次下载的进度显示至进度条,用于断点续传 currentProgress += lastProgress;//进度条为3个进度条总和, pb.setProgress(currentProgress); //发送消息,让主线程刷新文本进度 handler.sendEmptyMessage(1); fis.close(); } System.out.println("线程" + threadId + "的下载区间是:" + startIndex + "---" + endIndex); HttpURLConnection conn; URL url = new URL(path); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); //设置本次http请求所请求的数据的区间 conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); //请求部分数据,相应码是206 if(conn.getResponseCode() == 206){ //流里此时只有1/3原文件的数据 InputStream is = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; int total = 0; //拿到临时文件的输出流 File file = new File(Environment.getExternalStorageDirectory(), fileName); RandomAccessFile raf = new RandomAccessFile(file, "rwd"); //把文件的写入位置移动至startIndex raf.seek(startIndex); while((len = is.read(b)) != -1){ //每次读取流里数据之后,同步把数据写入临时文件 raf.write(b, 0, len); total += len; System.out.println("线程" + threadId + "下载了" + total); //每次读取流里数据之后,把本次读取的数据的长度显示至进度条 currentProgress += len; pb.setProgress(currentProgress);//进度条可以在子线程刷新UI //发送消息,让主线程刷新文本进度 handler.sendEmptyMessage(1); //生成一个专门用来记录下载进度的临时文件 RandomAccessFile progressRaf = new RandomAccessFile(progressFile, "rwd"); //每次读取流里数据之后,同步把当前线程下载的总进度写入进度临时文件中 progressRaf.write((total + "").getBytes()); progressRaf.close(); } System.out.println("线程" + threadId + "下载完毕-------------------小志参上!"); raf.close(); finishedThread++; synchronized (path) { if(finishedThread == ThreadCount){ for (int i = 0; i < ThreadCount; i++) { File f = new File(Environment.getExternalStorageDirectory(), i + ".txt"); f.delete(); } finishedThread = 0; } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4934926.html,如需转载请自行联系原作者