网络通信2-多线程下载网络资源
2015-02-03 19:33
162 查看
实现多线程下载网络资源重点在于网络资源相对于线程的分配。
需考虑以下问题:
1.每个线程下载哪一部分资源
2.而当一个线程下载时,其他线程从断点继续下载该资源
3.如何在同一文件保存每个线程所下载的资源
基于以上问题可以抽象出以下一个模型:
length:网络资源文件的大小(字节数)
thread_id:线程id
threadCount:线程数
taskCount:每个线程所下载的资源文件的字节数
length、threadCount、taskCount关系满足:
taskCount=length % threadCount == 0 ? length/threadCount : length/threadCount + 1;
(thread_id - 1)* taskCount:线程所下载资源文件的起始位置
thread_id * taskCount - 1:线程所下载资源文件的终止位置
首先获取资源文件的总的字节数length,指定下载线程数threadCount,每个给予一个编号thread_id(从1开始),编号thread_id的线程下载资源文件(thread_id
- 1)* taskCount位置到thread_id * taskCount- 1位置的字节,保存时新建字节数为length的文件,将每个线程下载的资源保存在指定位置.
演示:
线程类:
多线程下载:
需考虑以下问题:
1.每个线程下载哪一部分资源
2.而当一个线程下载时,其他线程从断点继续下载该资源
3.如何在同一文件保存每个线程所下载的资源
基于以上问题可以抽象出以下一个模型:
length:网络资源文件的大小(字节数)
thread_id:线程id
threadCount:线程数
taskCount:每个线程所下载的资源文件的字节数
length、threadCount、taskCount关系满足:
taskCount=length % threadCount == 0 ? length/threadCount : length/threadCount + 1;
(thread_id - 1)* taskCount:线程所下载资源文件的起始位置
thread_id * taskCount - 1:线程所下载资源文件的终止位置
首先获取资源文件的总的字节数length,指定下载线程数threadCount,每个给予一个编号thread_id(从1开始),编号thread_id的线程下载资源文件(thread_id
- 1)* taskCount位置到thread_id * taskCount- 1位置的字节,保存时新建字节数为length的文件,将每个线程下载的资源保存在指定位置.
演示:
线程类:
package com.test; import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class DownLoadThread extends Thread{ private File file; private int taskCount; private String path; private int thread_id; public DownLoadThread(File file, int taskCount, String path,int thread_id) { this.file = file; this.taskCount = taskCount; this.path = path; this.thread_id = thread_id; } public void run(){ int start = (thread_id - 1)* taskCount; int end = thread_id * taskCount - 1; try{ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); conn.setRequestProperty("Range", "bytes="+start+"-"+end); if(conn.getResponseCode() == 206){ InputStream in = conn.getInputStream(); RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rwd"); randomAccessFile.seek(start); byte[] buf = new byte[8192]; int len = 0; while((len = in.read(buf))!=-1){ randomAccessFile.write(buf,0,len); } in.close(); randomAccessFile.close(); System.out.println(thread_id+"号线程下载完毕!!!"); }else{ System.out.println("下载失败!!!"); } }catch(Exception e){ e.printStackTrace(); } } }
多线程下载:
package com.test; import java.io.File; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class Test { public static void main(String[] args) throws Exception{ download("http://localhost:8080/download/classloader.exe",3); } public static String getFileName(String path){ return path.substring(path.lastIndexOf("/")+1); } /** * 多线程下载网络资源 * @param string 指定的资源路径 * @param threadCount 指定的线程数量 */ public static void download(String path, int threadCount) throws Exception{ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); //获取网络资源文件的大小 int length = conn.getContentLength(); //本地创建一个文件 File file = new File(getFileName(path)); //设定本地文件的大小 与所下载文件大小一致 RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rwd"); randomAccessFile.setLength(length); randomAccessFile.close(); //计算下载线程的任务量 int taskCount = length % threadCount == 0 ? length/threadCount : length/threadCount + 1; //启动线程 下载对应资源 for(int thread_id = 1;thread_id <= threadCount;thread_id++){ new DownLoadThread(file,taskCount,path,thread_id).start(); } } }
相关文章推荐
- 多线程下载网络资源
- Java之多线程下载网络资源
- 多线程和网络通信实现的简单文件下载
- HttpUrlConnection实现多线程下载网络资源
- Java多线程下载网络资源
- java多线程下载网络资源(支持断点续传)
- 网络资源下载走向付费模式
- Android(五)数据存储之五网络多线程断点下载
- 网络多线程断点下载
- 网络多线程断点下载
- Linux Socket 网络编程 基于GTK+ 的多线程实现的局域网通信软件
- 调试posix多线程 socket网络通信有感
- android多线程断点下载——网络编
- 利用多线程实现网络通信
- [Android]网络资源下载时断点续传的实现
- 网络多线程断点下载
- C#网络通信(2)--TCP编程和多线程
- android多线程断点下载——网络编
- 常用软件下载地址(网络资源无限)