java多线程下载
来源:互联网 发布:药品销售数据查询 编辑:程序博客网 时间:2024/06/13 20:54
参考地址:
http://blog.csdn.net/qq_16293247/article/details/51554343
代码:
Main代码中:
package com.ecjtu.mutilThread;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;public class Main { public static final String PATH = "http://192.168.150.1:8088/txvpn_1.08.apk"; public static int threadCount=3; public static void main(String[] args) { try { URL url = new URL(PATH); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); if(connection.getResponseCode()==200){ int length=connection.getContentLength(); File file=new File("nihao.apk"); RandomAccessFile ref=new RandomAccessFile(file,"rwd"); ref.setLength(length); ref.close(); int size=length/threadCount; for(int id=0;id<threadCount;id++){ int startIndex=id*size; int endIndex=(id+1)*size-1; if(id==threadCount-1){ endIndex=length-1; } System.out.println("第"+id+"个线程的下载区间为"+startIndex+"-"+endIndex); new DownLoadThread(startIndex, endIndex, PATH, id).start(); } } } catch (IOException e) { e.printStackTrace(); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
在这个代码中主要就是实现上述步骤中的1、2、3的步骤,接下来的就是是实现一下IO流的操作即可。
DownLoadThread代码中:
package com.ecjtu.mutilThread;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;public class DownLoadThread extends Thread { private int startIndex,endIndex,threadId; private String path; public DownLoadThread(int startIndex,int endIndex,String path,int threadId) { this.startIndex=startIndex; this.endIndex=endIndex; this.path=path; this.threadId=threadId; } @Override public void run() { URL url; try { url=new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); connection.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex); if(connection.getResponseCode()==206){ InputStream is=connection.getInputStream(); File file=new File("nihao.zip"); RandomAccessFile ref=new RandomAccessFile(file,"rwd"); ref.seek(startIndex); byte[] b=new byte[1024]; int len=0; int total = 0; while((len=is.read(b))!=-1){ ref.write(b, 0, len); total+=len; System.out.println("第"+threadId+"条线程的下载"+total); } ref.close(); System.out.println("第"+threadId+"条线程的下载结束"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } super.run(); }}
0 0