使用HttpUrlConnection多线程下载文件
来源:互联网 发布:六年级英语软件 编辑:程序博客网 时间:2024/06/07 16:14
package com.example.administrator.myapplication;import java.io.IOException;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;/** * Created by Mr.MJ on 2017/1/22. */public class DownUtil { //定义下载的资源路径 private String path; //下载文件保存路径 private String targetFile; //定义下线线程条数 private int threadNum; //定义下载线程的对象 private DownThread[] downThreads; //下载文件大小 private int fileSize; public DownUtil(String path, String targetFile, int threadNum) { this.path = path; this.targetFile = targetFile; this.threadNum = threadNum; //初始化downThreads数组 downThreads=new DownThread[threadNum]; } public void download(){ 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,image/pjpeg," +"application/x-shockwave-flash,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("Acceppt-Language","zh-CN"); conn.setRequestProperty("Charset","UTF-8"); conn.setRequestProperty("Connection","Keep_Alive"); //得到文件的大小 fileSize= conn.getContentLength(); 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 currentPart=new RandomAccessFile(targetFile,"rw"); //定义该线程的下载位置 currentPart.seek(startPos); downThreads[i]=new DownThread(startPos,currentPartSize,currentPart); downThreads[i].start(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //计算进度条进度 public double getComplete(){ int sizeSum=0; for(int i=0;i<threadNum;i++){ sizeSum+=downThreads[i].length; } return sizeSum*1.0/fileSize; } private class DownThread extends Thread{ //当前线程的下载位置 private int startPos; //当前线程负责下载的文件大小 private int currentPartSize; public DownThread(int startPos,int currentPartSize,RandomAccessFile currentPart) { this.startPos = startPos; this.currentPartSize=currentPartSize; this.currentPart=currentPart; } //当前线程需要下载的文件块 private RandomAccessFile currentPart; //定义线程已下载的字节数 private int length; @Override public void 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,image/pjpeg," +"application/x-shockwave-flash,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("Acceppt-Language","zh-CN"); conn.setRequestProperty("Charset","UTF-8"); conn.setRequestProperty("Connection","Keep_Alive"); InputStream inputStream = conn.getInputStream(); skipFully(inputStream,startPos); byte[] buffer=new byte[1024]; int hasRead=0; while(length<currentPartSize&&(hasRead=inputStream.read(buffer))>0){ currentPart.write(buffer,0,hasRead); length+=hasRead; } currentPart.close(); inputStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } public static void skipFully(InputStream inStream,long bytes) throws IOException { long remainning=bytes; long len=0; while(remainning>0){ inStream.skip(remainning); remainning-=len; } }}
0 0
- 使用HttpUrlConnection多线程下载文件
- 使用HttpURLConnection下载文件
- 使用HttpURLConnection下载文件
- 使用HttpURLConnection下载文件
- 使用HttpURLConnection实现多线程下载
- 八.使用HttpURLConnection下载文件
- 使用HttpURLConnection实现文件下载
- Android 使用HttpURLConnection实现多线程下载 笔记
- android中使用HttpURLConnection实现多线程下载
- Android-使用HttpURLConnection实现多线程下载
- Java使用HttpUrlConnection实现多线程断点下载
- 使用greenDao+HttpurlConnection实现多线程断点下载
- HttpURLConnection多线程下载
- HttpURLConnection多线程下载
- HttpURLConnection实现多线程下载
- Java HttpUrlConnection多线程下载
- Android HttpURLConnection 多线程下载
- android多线程下载-HttpURLConnection
- JVM内幕:Java虚拟机详解
- jquery的checkbox,radio,select等方法总结
- android_90_开机动画
- 使用外部SMTP发送邮件
- __int64 与long long 的区别
- 使用HttpUrlConnection多线程下载文件
- 几句话实现导航栏透明渐变(iOS)
- android中的线程通信(二)
- the following classes could not be found android.support.v7.internal.
- Microsoft Excel.WsF——U盘里面的隐藏病毒怎么删除
- PAT(甲级)1005. Spell It Right (20)
- linux shell 关键字查找,返回OK 否则NO
- 2017年网站SEO站群类型推荐【干货分享】
- EasyUI combobox 的使用