码迷,mamicode.com
首页 > 编程语言 > 详细

网络多线线程下载示例

时间:2017-11-08 13:16:09      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:防止   ade   return   str   code   active   end   filter   throw   

网络多线线程下载:

IO流与多线程应用示例

  1 import priv.gdw.utils.FileUtils;
  2 import java.io.*;
  3 import java.net.URL;
  4 import java.net.URLConnection;
  5 
  6 /**
  7  * Created by kong on 07/11/2017.
  8  */
  9  
 10 public class Test02 {
 11 
 12     public static void main(String[] args) throws IOException {
 13         multithreadedDownload ("http://localhost:8080/image/a.jpg","/Users/Shared/abc",3);
 14     }
 15 
 16     /**
 17      * 多线程下载,仅适用于Http协议
 18      *
 19      * @param srcPath  下载源文件路径名
 20      * @param destPath 保存文件夹路径名
 21      * @param count    指定线程个数
 22      */
 23     public static void multithreadedDownload(String srcPath, String destPath, int count) throws IOException {
 24 
 25         //定义一个文件下载的文件夹路径
 26         File dir = new File (destPath);
 27         if (!dir.exists ()) {
 28             dir.mkdirs ();
 29         }
 30 
 31         long total = getFileLength (srcPath);
 32         OpenMultithreadedDownload (srcPath, dir, total, count);
 33 
 34         //判断线程的数量,如果只剩下一个,说明其他线程已经下载完成了
 35         while (Thread.activeCount () != 1) {
 36             try {
 37                 Thread.sleep (10);
 38             } catch (InterruptedException e) {
 39                 e.printStackTrace ();
 40             }
 41         }
 42         //获取文件的后缀名
 43         String suffix = srcPath.substring (srcPath.lastIndexOf ("."));
 44         //合并文件
 45         FileUtils.merge (dir, suffix);
 46 
 47         System.out.println ("文件下载完成");
 48     }
 49 
 50     /**
 51      * @param count 指定线程个数
 52      */
 53     private static void OpenMultithreadedDownload(String path, File dir, long total, int count) {
 54 
 55         //计算出每个线程平均现在的字节个数
 56         long size = total / count;
 57         //使用循环,计算每个线程下载开始和结束位置
 58         for (int i = 1; i <= count; i++) {
 59             //开始位置
 60             long starIndex = (i - 1) * size;
 61             //结束位置
 62             long endIndex = i * size - 1;
 63             //最后一个线程
 64             if (i == count) {
 65                 endIndex = total - 1;
 66             }
 67             DownloadThread dt = new DownloadThread (path, dir, starIndex, endIndex, i);
 68             dt.start ();
 69         }
 70     }
 71 
 72     //获取到服务器上的文件大小
 73     public static long getFileLength(String path) throws IOException {
 74 
 75         URL url = new URL (path);
 76         //获得连接对象
 77         URLConnection con = url.openConnection ();
 78         long total = con.getContentLengthLong ();
 79         return total;
 80     }
 81 }
 82 
 83 
 84 class DownloadThread extends Thread {
 85 
 86     private final String path;
 87     private final File dir;
 88     private final long startIndex;
 89     private final long endIndex;
 90     private final int threadID;
 91 
 92     public DownloadThread(String path, File dir, long startIndex,
 93                           long endIndex, int threadID) {
 94         this.path = path;
 95         this.dir = dir;
 96         this.startIndex = startIndex;
 97         this.endIndex = endIndex;
 98         this.threadID = threadID;
 99     }
100     @Override
101     public void run() {
102         try {
103             InputStream in = getInputStream ();
104             FileOutputStream fos = getFileOutputStream ();
105             int len ;
106             byte[] arr = new byte[8192];
107             while ((len = in.read (arr)) != -1){
108                 fos.write (arr,0,len);
109             }
110             in.close ();
111             fos.close ();
112         } catch (Exception e) {
113             e.printStackTrace ();
114         }
115 
116     }
117     //获取下载输出流
118     private FileOutputStream getFileOutputStream() throws FileNotFoundException {
119         //获取到文件的名字
120         int index = path.lastIndexOf ("/")+1;
121         String name = path.substring (index);
122         File file = new File (dir,threadID+name);//创建目标文件键,防止文件覆盖
123         //创建一个输出流
124         return new FileOutputStream (file);
125     }
126     //获取下载的输入流
127     private InputStream getInputStream() throws IOException {
128         //创建一个URL对象
129         URL url= new URL (path);
130         //获取到连接对象
131         URLConnection con = url.openConnection ();
132         con.setRequestProperty ("Range","bytes="+startIndex+"-"+endIndex);
133         //获取到输入流
134         return con.getInputStream ();
135     }
136 }

合并文件功能块:

 1 package priv.gdw.utils;
 2 
 3 import java.io.*;
 4 
 5 public class FileUtils {
 6 
 7     //合并文件功能
 8     public static void merge(File dir,String suffix) throws IOException {
 9 
10         File[] files = dir.listFiles (FileUtils.getFileFilter (suffix,false));
11         FileOutputStream fos = getFileOutputStream (dir, files[0]);
12 
13         for(File file : files){
14             FileInputStream fis = new FileInputStream (file);
15             int len;
16             byte[] buf = new byte[8192];
17             while ((len=fis.read (buf)) != -1){
18                 fos.write (buf,0,len);
19             }
20             fis.close ();
21             //把已经合并过的文件删除
22             file.delete ();
23         }
24         fos.close ();
25     }
26     //获取合并文件输出流
27     private static FileOutputStream getFileOutputStream(File dir, File file) throws FileNotFoundException {
28         File file1 = file;
29         String name = file1.getName ();
30         name = name.substring (1);
31         File destFile = new File (dir,name);
32         return new FileOutputStream (destFile);
33     }
34 
35 }

 

网络多线线程下载示例

标签:防止   ade   return   str   code   active   end   filter   throw   

原文地址:http://www.cnblogs.com/gdwkong/p/7803125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!