现在的位置: 首页 > 综合 > 正文

多线程下载

2014年02月25日 ⁄ 综合 ⁄ 共 4146字 ⁄ 字号 评论关闭

package www.zgl.net.down;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamTools {

 public static byte[] isToData(InputStream is) throws IOException{
  ByteArrayOutputStream bops = new ByteArrayOutputStream();

  byte buffer[] = new byte[1024];

  int len = 0;

  while ((len = is.read(buffer)) != -1) {
   bops.write(buffer, 0, len);
  }
  byte data[] = bops.toByteArray();
  bops.flush();
  bops.close();
  is.close();
  return data;
  
 }
}
---------------------------------------------------------------------------------------------------------------------------------------------

package www.zgl.net.down;

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 DownLoadTest {

 public File file;
 public RandomAccessFile accessFile;

 // 线程的数量
 int threadNum = 3;
 // 每个线程负责下载的大小
 int blockSize;
 // 创建访问的路径
 // public String path = "http://172.22.64.32:8080/doudou/youdao.exe";
 public String path = "http://172.22.64.32:8080/doudou/youdao.exe";

 public void testDown() {

  try {
   // 创建出URL对象
   URL url = new URL(path);
   // 创建出 HttpURLConnection对象
   HttpURLConnection httpURLConnection = (HttpURLConnection) url
     .openConnection();

   // 设置 发请求发送的方式
   httpURLConnection.setRequestMethod("GET");
   // 设置请求是否超时时间
   httpURLConnection.setConnectTimeout(5000);
   // 设置
   httpURLConnection
     .setRequestProperty("User-Agent",
       " Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)");

   // 是否响应成功
   if (httpURLConnection.getResponseCode() == 200) {
    // 获取文件的大小
    int size = httpURLConnection.getContentLength();
    System.out.println("文件的大小" + size);
    // 创建文件
    file = new File("youdao.exe");
    accessFile = new RandomAccessFile(file, "rwd");
    // 设置文件的大小
    accessFile.setLength(size);
    // 每个线程下载的大小
    blockSize = size / threadNum;
    // 开三个线程 操作此文件
    for (int i = 1; i <= threadNum; i++) {
     // 1 2 3
     // 计算出每个线程开始的位置
     int startSize = (i - 1) * blockSize;
     // 结束位置
     int endSize = (i) * blockSize;
     // 当线程是最后一个线程的时候
     if (i == threadNum) {
      // 判断文件的大小是否大于计算出来的结束位置
      if (size > endSize) {
       // 结束位置 等于 文件的大小
       endSize = size;
      }
     }
     // 为每个线程创建一个随机的读取
     RandomAccessFile threadAccessFile = new RandomAccessFile(
       file, "rwd");
     new Thread(new DownLoadThread(threadAccessFile, startSize,
       endSize, path)).start();
    }

   }

  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 public static void main(String[] args) {

  DownLoadTest downLoadTest = new DownLoadTest();
  // 调用下载方法
  downLoadTest.testDown();

 }
}

class DownLoadThread implements Runnable {

 // 下载文件的封装
 public RandomAccessFile accessFile;

 // 线程下载文件的起始位置
 public int startSize;
 public int endSize;

 // 文件下载的path路径
 public String path;

 public DownLoadThread(RandomAccessFile accessFile, int startSize,
   int endSize, String path) {

  this.accessFile = accessFile;
  this.startSize = startSize;
  this.endSize = endSize;
  this.path = path;
 }

 @Override
 public void run() {
  // 执行run方法
  try {
   // 创建URL对象
   URL url = new URL(path);
   // 创建HttpURLConnection对象
   HttpURLConnection httpURLConnection = (HttpURLConnection) url
     .openConnection();
   // 设置请求的头

   httpURLConnection.setRequestMethod("GET");
   // 设置请求是否超时时间
   httpURLConnection.setConnectTimeout(5000);
   // 设置
   httpURLConnection
     .setRequestProperty("User-Agent",
       " Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)");

   // 关键的设置
   httpURLConnection.setRequestProperty("Range", "bytes=" + startSize
     + "-" + endSize);

   // 输出当前线程
   System.out.println("当前线程" + Thread.currentThread().getName()
     + " 下载开始位置:" + startSize + " 下载结束位置:" + endSize);
   // 响应成功

   // 设置随机读取文件的 开始位置
   accessFile.seek(startSize);
   // 获取相应流对象
   InputStream is = httpURLConnection.getInputStream();
   // 创建输出流对象

   byte buffer[] = new byte[1024];

   int len = 0;

   while ((len = is.read(buffer)) != -1) {
    accessFile.write(buffer, 0, len);
   }
   accessFile.close();
   is.close();

   System.out.println(Thread.currentThread().getName() + "线程执行完毕");

  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}

 

抱歉!评论已关闭.