java多线程下载文件

来源:互联网 发布:物流信息软件 编辑:程序博客网 时间:2024/05/17 04:57
package com.thread.download;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;import java.text.SimpleDateFormat;import java.util.Date;public class MutilDownLoad {static String filepath = "D://aa.rmvb";static int threadCount = 3;static String path = "http://localhost:8080/aa.rmvb";public static void main(String[] args) {long a = System.currentTimeMillis();try {URL url = new URL(path);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(5000);connection.setReadTimeout(5000);if (connection.getResponseCode() == 200) {// 拿到文件的大小int contentLength = connection.getContentLength();File file = new File(filepath);// rwd不经过缓冲区,立刻直接写入硬盘 (断点续传)RandomAccessFile raf = new RandomAccessFile(file, "rwd");// 临时文件raf.setLength(contentLength);raf.close();// 计算每个文件应该下载多少个字节int size = contentLength / threadCount;for (int i = 0; i < threadCount; i++) {int startIndex = i * size;int endIndex = (i + 1) * size - 1;// 最后一个线程的结束位置if (i == threadCount - 1) {endIndex = contentLength - 1;}System.out.println("线程" + i + "的区间是" + startIndex + "结束的位置"+ endIndex);Runnable runnable = new DownLoadThread(startIndex,endIndex, i);Thread thread1 = new Thread(runnable);thread1.start();}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("\r<br>执行耗时 : " + (System.currentTimeMillis() - a)/ 1000f + " 秒 ");}}class DownLoadThread implements Runnable {int startIndex;int endIndex;int threadId;private String filepath = "D://aa.rmvb";public DownLoadThread(int startIndex, int endIndex, int threadId) {super();this.startIndex = startIndex;this.endIndex = endIndex;this.threadId = threadId;}@Overridepublic void run() {// TODO Auto-generated method stub// 再次发送http请求,下载源文件URL url;try {url = new URL(MutilDownLoad.path);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(5000);connection.setReadTimeout(5000);// 设置本次http的请求区间connection.setRequestProperty("Range", "bytes=" + startIndex + "-"+ endIndex);if (connection.getResponseCode() == 206) {InputStream is = connection.getInputStream();byte[] b = new byte[1024];int len = 0;int total = 0;// 拿到临时文件的输出流File file = new File(filepath);// rwd不经过缓冲区,立刻直接写入硬盘 (断点续传)RandomAccessFile raf = new RandomAccessFile(file, "rwd");raf.seek(startIndex);while ((len = is.read(b)) != -1) {raf.write(b, 0, len);total += len;Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String restime = sdf.format(date);System.out.println("线程" + threadId + "下载了多少" + total+ "当前时间=" + restime);}System.out.println("线程" + threadId + "下载完成");raf.close();}} catch (MalformedURLException | ProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

0 0
原创粉丝点击