多线程下载
步骤
多线程下载实现类
建立成员变量,描述多线程下载需要的参数
通过统一资源管理器URL,和要下载的资源建立连接
设置参数信息
获取网络资源的输入流,得到数据
创建文件存放的file对象
具体文件下载的实现
关闭流
多线程类
package com.zmc; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class ThreadDonwload implements Runnable{ private int begin;//开始位置 private int end;//结束位置 private int id;//线程编号 public ThreadDonwload(int begin, int end, int id) { super(); this.begin = begin; this.end = end; this.id = id; } @Override public void run() { try { //通过统一资源管理器URL,和要下载的资源建立连接 URL url = new URL(ThreadDownLoadTest.path); //获取联系 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //设置参数信息 urlConnection.setRequestMethod("GET");//get方式请求 urlConnection.setConnectTimeout(10000);//连接超时 urlConnection.setReadTimeout(10000);//读取超时 urlConnection.addRequestProperty("Range", "bytes="+begin+"-"+end);//设置读取的范围,固定格式的 System.out.println("线程"+Thread.currentThread().getName()+"--"+id+"下载的范围是"+begin+"---"+end); if(urlConnection.getResponseCode()==206) {//部分数据完成 //获取网络资源的输入流,得到数据 InputStream is = urlConnection.getInputStream(); //创建文件存放的file对象, File file = new File("D:\\AAAA\\zmc.zip"); //创建读写随机流 RandomAccessFile accessFile = new RandomAccessFile(file, "rws"); accessFile.seek(begin);//指定写入的开始位置 //具体文件下载的实现 byte[] b =new byte[8192]; int len = 0; while((len= is.read(b))!=-1) { accessFile.write(b, 0, len); } //关闭流 accessFile.close(); is.close(); //线程完成次数加1 ThreadDownLoadTest.finishNum++; //加锁 synchronized (ThreadDownLoadTest.path) { System.out.println("线程:"+Thread.currentThread().getName()+id+"下载完成" + ThreadDownLoadTest.finishNum); if(ThreadDownLoadTest.finishNum== ThreadDownLoadTest.threadNum) { //判断如果线程完成数量达到了线程数,重置成0 ThreadDownLoadTest.finishNum=0; } } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
主类
package com.zmc; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class ThreadDownLoadTest { static String path = "http://y.downya.com/soft/netfx20sp1x64_downyi.com.zip"; //线程的数量 static int threadNum=10; //已经下载完成的线程数量 static int finishNum=0; //通过统一资源管理器URL,和要下载的资源建立连接 public static void main(String[] args){ try { URL url = new URL(ThreadDownLoadTest.path); //获取联系 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //设置参数信息 urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(20000); urlConnection.setReadTimeout(20000); if (urlConnection.getResponseCode()==200) { //文件的长度 int length = urlConnection.getContentLength(); //每个线程需要下载的文件长度 int size = length/threadNum; for (int i = 0; i < threadNum; i++) { int begin=size*i; int end=size*(i+1); ThreadDonwload donwload = new ThreadDonwload(begin,end, i); Thread thread = new Thread(donwload); thread.start(); } } }catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }