Java之多线程断点下载的实现
发布时间:2020-12-14 23:38:10 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class MultiThread { private static int threadCount = 3; private static long blockSize; private static int runningThreadCount; public
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 public class MultiThread { private static int threadCount = 3; private static long blockSize; private static int runningThreadCount; public static void main(String[] args) throws Exception { String path = "http://10.0.67.172/test.exe"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000);//超时时间 int code = conn.getResponseCode(); System.out.println(code); if(code / 100 == 2){ int size = conn.getContentLength();//获取资源文件的长度 System.out.println("请求资源大小:" + size); blockSize = size / threadCount;//将资源文件分为多少块,没一块的大小 runningThreadCount = threadCount; long startIndex = 0; long endIndex = 0; //开启若干个子线程去实现多线程的下载 for(int i = 0; i < threadCount; i++){ startIndex = i * blockSize; endIndex = (i + 1) * blockSize - 1; if(i == threadCount-1){ endIndex = size - 1; } System.out.println("开启线程:" + i + ";" + "开始位置:" + startIndex + ":" + "结束位置:" + endIndex); new DownThread(path,startIndex,endIndex,i).start(); } } } private static class DownThread extends Thread{ private String path; private long startIndex; private long endIndex; private int threadId; public DownThread(String path,long startIndex,long endIndex,int threadId) { super(); this.path = path; this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; } @Override public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setReadTimeout(5000); conn.setRequestProperty("Range","bytes=" + startIndex + "-" + endIndex);//设置服务器上的文件的读取位置 int code = conn.getResponseCode(); if(code / 100 == 2){ InputStream is = conn.getInputStream(); File file = new File("temp.exe"); RandomAccessFile raf = new RandomAccessFile(file,"rw"); raf.seek(startIndex); System.out.println("第" + threadId + "个文件的开始位置:" + String.valueOf(startIndex)); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1){ raf.write(buffer,len);//写文件 } raf.close(); } } catch (Exception e) { e.printStackTrace(); } } } } public class MultiThread { private static int threadCount = 3; private static long blockSize; private static int runningThreadCount; public static void main(String[] args) throws Exception { String path = "http://10.0.67.172/test.rar"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000);//超时时间 int code = conn.getResponseCode(); System.out.println(code); if(code / 100 == 2){ int size = conn.getContentLength();//获取资源文件的长度 System.out.println("请求资源大小:" + size); blockSize = size / threadCount;//将资源文件分为多少块,没一块的大小 runningThreadCount = threadCount; long startIndex = 0; long endIndex = 0; for(int i = 0; i < threadCount; i++){ startIndex = i * blockSize; endIndex = (i + 1) * blockSize - 1; if(i == threadCount-1){ endIndex = size - 1; } System.out.println("开启线程:" + i + ";" + "开始位置:" + startIndex + ":" + "结束位置:" + endIndex); new DownThread(path,int threadId) { super(); this.path = path; this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; } @Override public void run() { int total = 0; try { File positionFile = new File(threadId + ".txt"); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); //接着上次的文件继续下载 if(positionFile.exists() && positionFile.length() > 0){ FileInputStream fis = new FileInputStream(positionFile); BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); //获取当前线程上次下载的总大小是多少 String lasttotalstr = reader.readLine(); int lastTotal = Integer.valueOf(lasttotalstr); System.out.println("上次线程下载的总大小:" + lastTotal); startIndex += lastTotal; total += lastTotal; fis.close(); } conn.setReadTimeout(5000); conn.setRequestProperty("Range","bytes=" + startIndex + "-" + endIndex);//设置服务器上的文件的读取位置 int code = conn.getResponseCode(); if(code / 100 == 2){ InputStream is = conn.getInputStream(); File file = new File("temp.rar"); RandomAccessFile raf = new RandomAccessFile(file,"rw"); raf.seek(startIndex); System.out.println("第" + threadId + "个文件的开始位置:" + String.valueOf(startIndex)); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1){ RandomAccessFile rf = new RandomAccessFile(positionFile,"rwd"); raf.write(buffer,len);//写文件 total += len; rf.write(String.valueOf(total).getBytes()); rf.close(); } is.close(); raf.close(); } } catch (Exception e) { e.printStackTrace(); }finally{ synchronized (DownThread.class) { System.out.println("线程" + threadId + "下载完毕了"); runningThreadCount--; if (runningThreadCount < 1) { System.out.println("所有的线程都工作完毕了。删除临时记录的文件"); for (int i = 0; i < threadCount; i++) { File f = new File(i + ".txt"); System.out.println(f.delete()); } } } } } } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- HDU 1024 Max Sum Plus Plus (动态规划、最大m子段和)
- Java 线程死锁的问题解决办法
- JVM | 分代垃圾回收策略的基本概念以及过程
- 在Java中将String转换为Date对象
- java – 如何安全地解析字符串?
- Java或C#中的UPnP AV远程控制
- 错误:java.io.IOException:错误的值类:类org.apache.had
- Java io包 ByteArrayInputStream&ByteArrayOutStream
- java – Android:Notification.DEFAULT_VIBRATE是否需要振
- 14-02 Java Math类,Random类,System类,BigDecimal类