multithreaddownload
来源:互联网 发布:大学生违法犯罪数据 编辑:程序博客网 时间:2024/06/16 12:15
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.security.acl.LastOwnerException;import javax.print.attribute.standard.Finishings;public class Main { static String path = "http://192.168.1.1/test.exe"; static int threadCount = 3; static int finishedThread = 0; public static void main(String[] args) { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(8000); conn.setReadTimeout(8000); if(conn.getResponseCode() == 200){ int length = conn.getContentLength(); //创建临时文件 File file = new File(getNameFromPath(path)); RandomAccessFile raf = new RandomAccessFile(file, "rwd"); //设置临时文件大小与目标文件一致 raf.setLength(length); raf.close(); //计算每个线程下载区间 int size = length / threadCount; for (int id = 0; id < threadCount; id++) { int startIndex = id * size; int endIndex = (id + 1) * size - 1; if(id == threadCount - 1){ endIndex = length - 1; } System.out.println("线程" + id + "下载的区间:" + startIndex + " ~ " + endIndex); new DownLoadThread(id, startIndex, endIndex).start(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String getNameFromPath(String path){ int index = path.lastIndexOf("/"); return path.substring(index + 1); }}class DownLoadThread extends Thread{ int threadId; int startIndex; int endIndex; public DownLoadThread(int threadId, int startIndex, int endIndex) { super(); this.threadId = threadId; this.startIndex = startIndex; this.endIndex = endIndex; } @Override public void run() { try { File fileProgress = new File(threadId + ".txt"); int lastProgress = 0; if(fileProgress.exists()){ FileInputStream fis = new FileInputStream(fileProgress); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); //得到上一次下载进度 lastProgress = Integer.parseInt(br.readLine()); startIndex += lastProgress; fis.close(); } URL url = new URL(Main.path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(8000); conn.setReadTimeout(8000); //设置请求数据的区间 conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); if(conn.getResponseCode() == 206){ InputStream is = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; //当前下载的总进度 int total = lastProgress; File file = new File(Main.getNameFromPath(Main.path)); RandomAccessFile raf = new RandomAccessFile(file, "rwd"); raf.seek(startIndex); while((len = is.read(b)) != -1){ raf.write(b, 0, len); total += len; System.out.println(threadId + "下载了:" + total); //创建一个进度临时文件,保存下载进度 RandomAccessFile rafProgress = new RandomAccessFile(fileProgress, "rwd"); rafProgress.write((total + "").getBytes()); rafProgress.close(); } raf.close(); System.out.println(threadId + "下载完毕------------------"); //3部分全部下载完毕,才删 Main.finishedThread++; synchronized (Main.path) { if(Main.finishedThread == Main.threadCount){ for (int i = 0; i < Main.threadCount; i++) { File f = new File(i + ".txt"); f.delete(); } Main.finishedThread = 0; } } } } catch (Exception e) { e.printStackTrace(); } }}
0 0
- MultiThreadDownLoad
- multithreaddownload
- MultiThreadDownLoad多线程下载
- 高可靠芯片搭配视觉演算法,影像式ADAS满足车规要求
- MySQL5.6源码编译安装(CentOS-6.6+MySQL-5.6)(一)
- Problem A: 假金币
- 页面上现有ProgressBar控件progressBar,请用书写线程以10秒的的时间完成其进度显示工作。
- 安卓开发之java基础笔记7内部类与设计模式
- multithreaddownload
- 总结一下适合自己的看书方式
- 2016 "百度之星" - 资格赛(Astar Round1)
- C++类型操作
- Antlr代码生成及命令行验证
- 请继承SQLiteOpenHelper实现
- 服务器1M带宽同时能承受多少人在线
- 内存泄露分析
- POJ 3237Tree 树链剖分