首页 > 代码库 > 使用HttpURLConnection多线程下载
使用HttpURLConnection多线程下载
1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.io.RandomAccessFile; 4 import java.net.HttpURLConnection; 5 import java.net.MalformedURLException; 6 import java.net.ProtocolException; 7 import java.net.URL; 8 9 /**10 * 线程类11 * @author Administrator12 *13 */14 public class DownThread extends Thread{15 16 private String path;17 private int startPos;18 private int currentPartSize;19 private RandomAccessFile currentPart;20 21 public int length;22 23 public DownThread(String path, int startPos, int currentPartSize,24 RandomAccessFile currentPart) {25 26 this.path = path;27 this.startPos = startPos;28 this.currentPartSize = currentPartSize;29 this.currentPart = currentPart;30 }31 32 @Override33 public void run() {34 try {35 URL url = new URL(path);36 HttpURLConnection conn;37 conn = (HttpURLConnection)url.openConnection();38 /* 设置超时时长 */39 conn.setConnectTimeout(5000);40 /* 设置发送请求的方法 */41 conn.setRequestMethod("GET");42 43 conn.setRequestProperty("Accept", "image/gif, image/x-xbitmap, " +44 "image/jpeg, image/pjpeg, application/x-shockwave-flash, " +45 "application/vnd.ms-excel, application/vnd.ms-powerpoint, " +46 "application/msword, application/x-silverlight, */*");47 48 conn.setRequestProperty("Accept-Language", "zh-CN");49 conn.setRequestProperty("Charset", "UTF-8");50 /* 返回该Socket对象对应的输出流,让程序通过该输入流从Socket中取出数据 */51 InputStream inStream = conn.getInputStream();52 inStream.skip(startPos);//寻到开始下载的位置53 byte[] buffer = new byte[1024];54 int hasRead = 0;55 while(length < currentPartSize && (hasRead = inStream.read(buffer)) > 0){56 currentPart.write(buffer, 0, hasRead);57 length += hasRead;58 }59 currentPart.close();60 inStream.close();61 } catch (MalformedURLException e) {62 e.printStackTrace();63 } catch (ProtocolException e) {64 e.printStackTrace();65 } catch (IOException e) {66 e.printStackTrace();67 }68 }69 70 71 }
1 import java.io.RandomAccessFile; 2 import java.net.HttpURLConnection; 3 import java.net.URL; 4 5 6 public class DownUtil { 7 /* 下载资源的路径--url */ 8 private String path; 9 private String targetFile;10 private int threadNum;11 /* 多线程下载 */12 private DownThread[] threads;13 private int fileSize;14 public DownUtil(String path, String targetFile, int threadNum) {15 16 this.path = path;17 this.targetFile = targetFile;18 threads = new DownThread[threadNum];19 this.threadNum = threadNum;20 }21 22 public void download() throws Exception{23 URL url = new URL(path);24 HttpURLConnection conn;25 conn = (HttpURLConnection)url.openConnection();26 27 conn.setConnectTimeout(5000);28 29 conn.setRequestMethod("GET");30 31 conn.setRequestProperty("Accept", "image/gif, image/x-xbitmap, " +32 "image/jpeg, image/pjpeg, application/x-shockwave-flash, " +33 "application/vnd.ms-excel, application/vnd.ms-powerpoint, " +34 "application/msword, application/x-silverlight, */*");35 36 conn.setRequestProperty("Accept-Language", "zh-CN");37 conn.setRequestProperty("Charset", "UTF-8");38 conn.setRequestProperty("Connection", "Keep-Alive");39 /* 得到文件的大小 */40 fileSize = conn.getContentLength();41 conn.disconnect();42 int currentPartSize = fileSize / threadNum + 1;43 RandomAccessFile file = new RandomAccessFile(targetFile, "rw");44 /* 设置本地文件的大小 */45 file.setLength(fileSize);46 file.close();47 48 for (int i = 0; i < threadNum; i++) {49 int startPos = i * currentPartSize;50 /* 每个线程使用一个RandomAccessFile进行下载 */51 RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");52 /* 定位文件下载的位置*/53 currentPart.seek(startPos);54 threads[i] = new DownThread(path, startPos, currentPartSize, currentPart);55 threads[i].start();56 }57 }58 59 public double getCompleteRate(){60 int sumSize = 0;61 for (int i = 0; i < threadNum; i++) {62 sumSize += threads[i].length;63 }64 return sumSize * 1.0 / fileSize;65 }66 }
使用HttpURLConnection多线程下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。