免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1019 | 回复: 0
打印 上一主题 下一主题

多线程下载 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-01 09:57 |只看该作者 |倒序浏览
多个线程同时下载网络文件,基于http协议
[Java]代码
  1. package com.test.download;

  2. import java.io.File;
  3. import java.io.InputStream;
  4. import java.io.RandomAccessFile;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;

  7. /*
  8. * 多线程下载
  9. */
  10. public class MulThreadDownload {

  11.     public static void main(String[] args) throws Exception {
  12.         String path = "http://pic.4j4j.cn/upload/pic/20130909/681ebf9d64.jpg";
  13.         new MulThreadDownload().download(path,3);
  14.     }
  15.      
  16.     public void download (String path,int threadsize) throws Exception{
  17.         URL url = new URL(path);
  18.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  19.         conn.setConnectTimeout(5000);
  20.         conn.setRequestMethod("GET");
  21.         if(conn.getResponseCode() == 200){
  22.             //获取网络文件长度
  23.             int length = conn.getContentLength();   
  24.             //新建本地文件保存下载数据
  25.             File file = new File(getFilename(path));
  26.             //计算每条线程负责下载的数据量
  27.             int block = length%threadsize==0 ? length/threadsize : length/threadsize+1;
  28.             //开启指定数目的线程同时下载
  29.             for(int threadid = 0; threadid < threadsize; threadid++){
  30.                 new DownloadThread(threadid,block,url,file).start();
  31.             }
  32.         }else{
  33.             System.out.println("下载失败!");
  34.         }
  35.     }
  36.      
  37.     private class DownloadThread extends Thread{
  38.         private int threadid;   //线程编号
  39.         private int block;      //下载块大小
  40.         private URL url;        //下载链接
  41.         private File file;      //下载数据保存文件
  42.         public DownloadThread(int threadid, int block, URL url, File file) {
  43.             this.threadid = threadid;
  44.             this.block = block;
  45.             this.url = url;
  46.             this.file = file;
  47.         }

  48.         public void run() {
  49.             int start = threadid * block;       //本线程下载数据写入文件开始位置
  50.             int end = (threadid+1) * block - 1; //本线程下载数据写入文件结束位置
  51.             try {
  52.                 //创建一个随机访问文件流对象
  53.                 RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
  54.                 //文件指针偏移至正确写入位置
  55.                 accessFile.seek(start);
  56.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  57.                 conn.setConnectTimeout(5000);
  58.                 conn.setRequestMethod("GET");
  59.                 //设置请求数据的范围
  60.                 conn.setRequestProperty("Range", "bytes="+start+"-"+end);
  61.                 if(conn.getResponseCode() == 206){//状态码206:(部分内容) 服务器成功处理了部分 GET 请求
  62.                     InputStream inStream = conn.getInputStream();
  63.                     byte[] buffer = new byte[1024];
  64.                     int len = 0;
  65.                     while((len = inStream.read(buffer)) != -1){
  66.                         accessFile.write(buffer, 0, len);
  67.                     }
  68.                     accessFile.close();
  69.                     inStream.close();
  70.                 }
  71.                 System.out.println("第"+(threadid+1)+"部分下载完成");
  72.             } catch (Exception e) {
  73.                 e.printStackTrace();
  74.             }
  75.         }
  76.     }

  77.     private String getFilename(String path) {
  78.         return path.substring(path.lastIndexOf("/")+1);
  79.     }
  80. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP