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

使用HttpURLConnection多线程下载

时间:2014-10-23 20:33:38      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   使用   java   

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import java.io.RandomAccessFile;
 4 import java.net.HttpURLConnection;
 5 import java.net.MalformedURLException;
 6 import java.net.ProtocolException;
 7 import java.net.URL;
 8 
 9 /**
10  * 线程类
11  * @author Administrator
12  *
13  */
14 public class DownThread extends Thread{
15     
16     private String path;
17     private int startPos;
18     private int currentPartSize;
19     private RandomAccessFile currentPart;
20     
21     public int length;
22     
23     public DownThread(String path, int startPos, int currentPartSize,
24             RandomAccessFile currentPart) {
25         
26         this.path = path;
27         this.startPos = startPos;
28         this.currentPartSize = currentPartSize;
29         this.currentPart = currentPart;
30     }
31 
32     @Override
33     public void run() {
34         try {
35             URL url = new URL(path);
36             HttpURLConnection conn;
37             conn = (HttpURLConnection)url.openConnection();
38             /* 设置超时时长 */
39             conn.setConnectTimeout(5000);
40             /* 设置发送请求的方法 */
41             conn.setRequestMethod("GET");
42             
43             conn.setRequestProperty("Accept", "image/gif, image/x-xbitmap, " +
44                     "image/jpeg, image/pjpeg, application/x-shockwave-flash, " +
45                     "application/vnd.ms-excel, application/vnd.ms-powerpoint, " +
46                     "application/msword, application/x-silverlight, */*");
47             
48             conn.setRequestProperty("Accept-Language", "zh-CN");
49             conn.setRequestProperty("Charset", "UTF-8");
50             /* 返回该Socket对象对应的输出流,让程序通过该输入流从Socket中取出数据 */
51             InputStream inStream = conn.getInputStream();
52             inStream.skip(startPos);//寻到开始下载的位置
53             byte[] buffer = new byte[1024];
54             int hasRead = 0;
55             while(length < currentPartSize && (hasRead = inStream.read(buffer)) > 0){
56                 currentPart.write(buffer, 0, hasRead);
57                 length += hasRead;
58             }
59             currentPart.close();
60             inStream.close();
61         } catch (MalformedURLException e) {
62             e.printStackTrace();
63         } catch (ProtocolException e) {
64             e.printStackTrace();
65         } catch (IOException e) {
66             e.printStackTrace();
67         }
68     }
69     
70     
71 }
 1 import java.io.RandomAccessFile;
 2 import java.net.HttpURLConnection;
 3 import java.net.URL;
 4 
 5 
 6 public class DownUtil {
 7     /* 下载资源的路径--url */
 8     private String path;
 9     private String targetFile;
10     private int threadNum;
11     /* 多线程下载 */
12     private DownThread[] threads;
13     private int fileSize;
14     public DownUtil(String path, String targetFile, int threadNum) {
15         
16         this.path = path;
17         this.targetFile = targetFile;
18         threads = new DownThread[threadNum];
19         this.threadNum = threadNum;
20     }
21     
22     public void download() throws Exception{
23         URL url = new URL(path);
24         HttpURLConnection conn;
25         conn = (HttpURLConnection)url.openConnection();
26     
27         conn.setConnectTimeout(5000);
28         
29         conn.setRequestMethod("GET");
30         
31         conn.setRequestProperty("Accept", "image/gif, image/x-xbitmap, " +
32                 "image/jpeg, image/pjpeg, application/x-shockwave-flash, " +
33                 "application/vnd.ms-excel, application/vnd.ms-powerpoint, " +
34                 "application/msword, application/x-silverlight, */*");
35         
36         conn.setRequestProperty("Accept-Language", "zh-CN");
37         conn.setRequestProperty("Charset", "UTF-8");
38         conn.setRequestProperty("Connection", "Keep-Alive");
39         /* 得到文件的大小 */
40         fileSize = conn.getContentLength();
41         conn.disconnect();
42         int currentPartSize = fileSize / threadNum + 1;
43         RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
44         /* 设置本地文件的大小 */
45         file.setLength(fileSize);
46         file.close();
47         
48         for (int i = 0; i < threadNum; i++) {
49             int startPos = i * currentPartSize;
50             /* 每个线程使用一个RandomAccessFile进行下载 */
51             RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
52             /* 定位文件下载的位置*/
53             currentPart.seek(startPos);
54             threads[i] = new DownThread(path, startPos, currentPartSize, currentPart);
55             threads[i].start();
56         }
57     }
58     
59     public double getCompleteRate(){
60         int sumSize = 0;
61         for (int i = 0; i < threadNum; i++) {
62             sumSize += threads[i].length;
63         }
64         return sumSize * 1.0 / fileSize;
65     }
66 }

 

使用HttpURLConnection多线程下载

标签:style   blog   http   color   io   os   ar   使用   java   

原文地址:http://www.cnblogs.com/wanghui390/p/4046575.html

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