zoukankan      html  css  js  c++  java
  • 多线程从网络下载文件

    主类:

     1 public class DownThreadDemo {
     2 
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5         int threadNum = 3;
     6         long fileLen = 0;
     7         long numPerThread = 0;
     8         long left = 0;
     9         URL url = null;
    10         URLConnection connection = null;
    11         String fileName = "e:\temp\downlo.jpg";
    12         RandomAccessFile[] raf = new RandomAccessFile[threadNum];
    13         InputStream[] is = new InputStream[threadNum];
    14         try {
    15             url = new URL(
    16                     "http://img.bizhi.sogou.com/images/2014/08/22/830201.jpg");
    17             // 打开网路连接
    18             connection = url.openConnection();
    19             // 计算文件大小
    20             fileLen = connection.getContentLengthLong();
    21             // 每个线程处理的文件大小
    22             numPerThread = fileLen / threadNum;
    23             // 最后一块
    24             left = fileLen % threadNum;
    25             // openConnection().getInputStream()
    26             is[0] = url.openStream();
    27             raf[0] = new RandomAccessFile(fileName, "rw");
    28             raf[0].setLength(fileLen);
    29             // 分别为每个线程创建一个从网络读取的流与写入文件的流。
    30             for (int i = 0; i < threadNum; i++) {
    31                 if (i != 0) {
    32                     is[i] = url.openStream();
    33                     raf[i] = new RandomAccessFile(fileName, "rw");
    34                 }
    35                 if (i == threadNum - 1) {
    36                     new MultyDown(i * numPerThread, (i + 1) * numPerThread
    37                             + left, is[i], raf[i]).start();
    38                 } else {
    39                     new MultyDown(i * numPerThread, (i + 1) * numPerThread,
    40                             is[i], raf[i]).start();
    41                 }
    42             }
    43         } catch (MalformedURLException e) {
    44             // TODO Auto-generated catch block
    45             e.printStackTrace();
    46         } catch (IOException e) {
    47             // TODO Auto-generated catch block
    48             e.printStackTrace();
    49         }
    50         System.out.println("end------");
    51     }
    52 
    53 }

    线程类:

     1 public class MultyDown extends Thread {
     2     private long start, end;
     3     private InputStream is;
     4     private RandomAccessFile raf;
     5 
     6     public MultyDown() {
     7         // TODO Auto-generated constructor stub
     8     }
     9 
    10     public MultyDown(long start, long end, InputStream is, RandomAccessFile raf) {
    11         // TODO Auto-generated constructor stub
    12         this.start = start;
    13         this.end = end;
    14         this.is = is;
    15         this.raf = raf;
    16     }
    17 
    18     @Override
    19     public void run() {
    20         // TODO Auto-generated method stub
    21 
    22         int len = 0;
    23         int size = 0;
    24         byte[] b = new byte[24];
    25         try {
    26             // 跳到文件指定位置
    27             is.skip(start);
    28             raf.seek(start);
    29             while ((len = is.read(b)) != -1 && size < (end - start)) {
    30                 raf.write(b, 0, len);
    31                 size += len;
    32             }
    33         } catch (IOException e) {
    34             // TODO Auto-generated catch block
    35             e.printStackTrace();
    36         } finally {
    37             if (is != null) {
    38                 try {
    39                     is.close();
    40                 } catch (IOException e) {
    41                     // TODO Auto-generated catch block
    42                     e.printStackTrace();
    43                 }
    44             }
    45             if (raf != null) {
    46                 try {
    47                     raf.close();
    48                 } catch (IOException e) {
    49                     // TODO Auto-generated catch block
    50                     e.printStackTrace();
    51                 }
    52             }
    53         }
    54     }
    55 
    56 }
  • 相关阅读:
    分享:两个栈实现一个队列的功能
    分享:要关注技术,但也要关注所做的事情
    linux网络编程中阻塞和非阻塞socket的区别
    分享:C++参数传递方式
    linux非阻塞socket教程
    有用和有趣的产品秤砣
    查找 grep r TFramedTransport *
    分享:SecureCRT使用VIM语法高亮
    linux 非阻塞 socket Google 搜索
    linux c语言 select函数用法 ITeye技术网站
  • 原文地址:https://www.cnblogs.com/mada0/p/4719110.html
Copyright © 2011-2022 走看看