java多线程下载

来源:互联网 发布:药品销售数据查询 编辑:程序博客网 时间:2024/06/13 20:54

参考地址:

http://blog.csdn.net/qq_16293247/article/details/51554343


代码:

Main代码中:

package com.ecjtu.mutilThread;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;public class Main {    /*     * 先拿到需要下载文件的大小,先不用拿到流     */    public static final String PATH = "http://192.168.150.1:8088/txvpn_1.08.apk";    public static int threadCount=3;///进行下载的数量    public static void main(String[] args) {        try {            URL url = new URL(PATH);            ///在这里用HttpURLConnection只是用来获取文件的大小            HttpURLConnection connection = (HttpURLConnection) url.openConnection();            connection.setRequestMethod("GET");            connection.setConnectTimeout(8000);            connection.setReadTimeout(8000);            if(connection.getResponseCode()==200){                ///在这里就可以拿到文件的大小                int length=connection.getContentLength();                ///在这里先占个位置,也就是创建一个文件的大小的区间                File file=new File("nihao.apk");                ///随机文件,让该文件拥有需下载的文件的大小,从而达到占位置的作用                ///在这里的第二个参数一班为rwd,不仅让他可读可写,并且会将数据刻在硬件上                RandomAccessFile ref=new RandomAccessFile(file,"rwd");                 ref.setLength(length);                ref.close();                int size=length/threadCount;                for(int id=0;id<threadCount;id++){                    //1.确定每个线程的下载区间                    //2.确定完后,开始对应的子线程                    ///在这里确定开始位置和结束位置                    int startIndex=id*size;                    int endIndex=(id+1)*size-1;                    ///将最后一个线程的结束为止拿出单独,将剩余的长度全给最后一个                    if(id==threadCount-1){                        endIndex=length-1;                    }                    System.out.println("第"+id+"个线程的下载区间为"+startIndex+"-"+endIndex);                    new DownLoadThread(startIndex, endIndex, PATH, id).start();                }            }        } catch (IOException e) {            e.printStackTrace();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

在这个代码中主要就是实现上述步骤中的1、2、3的步骤,接下来的就是是实现一下IO流的操作即可。

DownLoadThread代码中:

package com.ecjtu.mutilThread;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;public class DownLoadThread extends Thread {    private int startIndex,endIndex,threadId;    private String path;    public DownLoadThread(int startIndex,int endIndex,String path,int threadId) {        this.startIndex=startIndex;        this.endIndex=endIndex;        this.path=path;        this.threadId=threadId;    }    @Override    public void run() {        URL url;        try {                      ///这里才是利用HttpURLConnection拿到需要下载的真正的输入流            url=new URL(path);            HttpURLConnection connection = (HttpURLConnection) url.openConnection();            connection.setRequestMethod("GET");            connection.setConnectTimeout(8000);            connection.setReadTimeout(8000);            ///在这里用HttpURLConnection的设置请求属性的方法,来请求获取每一段的信息(在这里的格式书写是一样的,固定的)。            connection.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);            ///这里的206是代表上面的区段请求的响应的返回码            if(connection.getResponseCode()==206){                ///接下就是输入流的一些操作                InputStream is=connection.getInputStream();                File file=new File("nihao.zip");                RandomAccessFile ref=new RandomAccessFile(file,"rwd");                 ref.seek(startIndex);///标志开始位置,可以让线程在文件中从不同位置开始存储                byte[] b=new byte[1024];                int len=0;                int total = 0;                while((len=is.read(b))!=-1){                    ref.write(b, 0, len);                    total+=len;                    System.out.println("第"+threadId+"条线程的下载"+total);                }                ref.close();                System.out.println("第"+threadId+"条线程的下载结束");            }        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (ProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        super.run();    }}

0 0