多线程下载

来源:互联网 发布:美工设计网站大全 编辑:程序博客网 时间:2024/06/07 04:10
<span style="font-size:18px;"></span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">下载:</span>
<span style="font-size:18px;">1、用Http去请求服务器</span>
<span style="font-size:18px;">2、获取文件的大小</span>
<span style="font-size:18px;">3、创建一个空的文件夹与服务器三文件等大小</span>
<span style="font-size:18px;">4、根据线程的总数,计算每一个线程需要下载的区间段</span>
<span style="font-size:18px;">5、每个线程下载以后写入到本地文件的指定单位位置</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">package com.hx.zy.download;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;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.URL;import java.net.URLConnection;import javax.tools.FileObject;import com.hx.zy.constant.Contant;public class TestDownload {//public static int threadCount = 3;public static int blockSize = 0;public static void main(String[] args) throws Exception {URL url = new URL("http://localhost:8080/download/cc.zip");HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setReadTimeout(10000);long length = conn.getContentLength();conn.connect(); //连接File file = new File("cc.zip");RandomAccessFile randomFile = new RandomAccessFile(file,"rwd");randomFile.setLength(length);//每一个线程他需要下载的大小System.out.println("总大小:" + length);blockSize = (int) (length / Contant.threadCount);System.out.println("每一个线程下载的大小:" + blockSize);long start = 1;long end = 0;for(int i=1; i < (Contant.threadCount+1); i++){//1 》》 0 - 46354938 123456// 2 >> 46354939 -(46354939 + 46354938)//3 》》 start = blockSize * (i -1);end = blockSize*i -1;if(i == Contant.threadCount){end = length;}System.out.println("第" + i + "个线程下载的区间:" + start + " - " + end);new downloadThread(file, i, start, end).start();}conn.disconnect();}}class downloadThread extends Thread{private File path; //本地磁盘的地址private String url = "http://localhost:8080/download/cc.zip" ; //HTTP请求的地址private Integer threadID; //当前是第几个线程private long start; //该线程下载的起始位置private long end; //该线程的结束位置public downloadThread(File path, Integer threadID, long start,long end) {this.path = path;this.threadID = threadID;this.start = start;this.end = end;}@Overridepublic void run() {try {System.out.println(threadID + "开启读取");File contextFile = new File(threadID +".txt");if(!contextFile.exists()){contextFile.createNewFile();}//读取断线信息里面的数据来重新的指定从哪里开始写BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(contextFile)));String info = reader.readLine();if(null != info && info.length() > 0){start = start + Integer.parseInt(info);}System.out.println("start........=" + start);URL uri = new URL(url);HttpURLConnection conn = (HttpURLConnection) uri.openConnection();conn.setRequestProperty("Range", "bytes="+start +"-" + end);conn.setReadTimeout(50000);conn.setConnectTimeout(50000);conn.connect();//让http请求只请求一个片段System.out.println("bytes="+start +"-" + end);//判断一下是否为200?int status = conn.getResponseCode();if(status / 100 == 2){int total = 0; //总共下载的字节数//FileOutputStream out = new FileOutputStream(new File(threadID +".txt")); 不靠谱了RandomAccessFile random = new RandomAccessFile(path, "rw");random.seek(start);InputStream in = conn.getInputStream();byte[] buffer = new byte[1024];int len = 0;while((len = in.read(buffer)) != -1){RandomAccessFile duandian = new RandomAccessFile(contextFile, "rwd");random.write(buffer, 0, len);total += len;duandian.write(String.valueOf(total).getBytes());duandian.close();}in.close();random.close();}conn.disconnect();System.out.println(threadID + "读取完成");} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{synchronized (TestDownload.class){Contant.threadCount --;System.out.println("Contant.threadCount = " + Contant.threadCount);if(Contant.threadCount == 0){for(int i=1; i < 4; i++){File contextFile = new File(i +".txt");System.out.println(contextFile.delete() +"   ccc");}}}}}}</span>

0 0
原创粉丝点击