HttpUrlConnection实现多线程下载网络资源
2016-11-02 15:55
549 查看
/** * * @create_time:2016年11月1日 下午4:45:05 * @类描述:多线程下载工具类 * @version: * */ public class DownUtil { private String path;//下载资源的路径 private String targetFile;//下载文件的保存位置 private int threadNum;//线程数 private DownloadThread[] threads; private int fileSize;//文件大小 public DownUtil(String path,String targetFile,int threadNum){ this.path = path; this.targetFile = targetFile; this.threadNum = threadNum; threads = new DownloadThread[threadNum];//初始化threads数据 } /** *下载 */ public void download() throws Exception{ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5*1000); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "image/gif,image/jpeg,image/pjpeg,application/x-shockwaveflash,application/xaml+xml,application/vnd.ms-xpsdocument,application/x-ms-xbap,application/x-ms-application,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 7.0;Windows NT 5.2;Trident/4.0;.NET CLR 1.1.4322;.NET CLR 2.0.50727;.NET CLR 3.0.04506;.NET CLR 3.0.4506.2152;.NET CLR 3.5.30729)"); conn.setRequestProperty("Connection", "Keep-Alive"); fileSize = conn.getContentLength();//获取文件大小 conn.disconnect(); int currentPartSize = fileSize/threadNum +1; RandomAccessFile file = new RandomAccessFile(targetFile, "rw"); file.setLength(fileSize); file.close(); for (int i = 0; i < threadNum; i++) { //每个线程下载的起始位置 int startPos = i*currentPartSize; //每一个线程使用一个RandomAccessFile进行下载 RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw"); //定位该线程的下载位置 currentPart.seek(startPos); threads[i] = new DownloadThread(startPos, currentPartSize, currentPart); threads[i].start(); } } //获取下载完成的百分比 public double getCompleteRate(){ int sumSize = 0; for (int i = 0; i < threadNum; i++) { sumSize +=threads[i].length; } return sumSize*1.0/fileSize; } private class DownloadThread extends Thread{ private int startPos;//当前线程的下载位置 private int currentPartSize; //当前线程需要下在的文件块 private RandomAccessFile currentPart; //定义已下载的字节数 public int length; public DownloadThread(int startPos,int currentPartSize,RandomAccessFile currentPart){ this.startPos = startPos; this.currentPart = currentPart; this.currentPartSize = currentPartSize; } @Override public void run() { super.run(); try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5*1000); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "image/gif,image/jpeg,image/pjpeg,application/x-shockwaveflash,application/xaml+xml,application/vnd.ms-xpsdocument,application/x-ms-xbap,application/x-ms-application,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Charset", "UTF-8"); InputStream is = conn.getInputStream(); is.skip(this.startPos); byte[] buffer = new byte[1024]; int hasRead = 0; while (length<currentPartSize&& (hasRead = is.read(buffer))!=-1) { currentPart.write(buffer); length +=hasRead; } currentPart.close(); is.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
相关文章推荐
- HttpURLConnection实现多线程网络下载
- Java使用HttpUrlConnection实现多线程断点下载
- android网络编程之HttpUrlConnection的讲解--实现文件断点下载
- 运用java.net.HttpURLConnection实现java多线程下载文件
- Android移动开发-使用HttpURLConnection实现多线程的下载
- 使用greenDao+HttpurlConnection实现多线程断点下载
- Java使用HttpUrlConnection实现多线程断点下载
- 使用HttpURLConnection实现多线程下载
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- HttpURLConnection实现下载功能
- handler、HttpURLConnection、网络数据下载综合使用。
- 问答邮件整理记录——HttpURLConnection下载网络文件
- Android通过HttpURLConnection和HttpClient接口实现网络编程
- Fragment中存放GridView,结合LruCache、DiskLruCache和HttpURLConnection实现图片下载、缓存
- Android通过HttpURLConnection和HttpClient接口实现网络编程
- 通用网络请求实现之HttpURLConnection和OkHttp
- 下载网络文件HttpURLConnection.getContentLength()大小为 -1
- Android中如何使用HttpURLConnection实现GET POST JSON数据与下载图片
- 使用HttpURLConnection实现图片的下载与现显示
- AsyncHttpClient实现上传 HttpURLConnection实现下载