zoukankan      html  css  js  c++  java
  • 当年一个简单可用的多线程断点续传类

    代码如下:

    package com;
    
    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.URL;
    
    /**
    *这个类已经实现了多线程断点下载的功能,每次运行都会从进度文件中读取进度接着下,下完了会把进度文件删除掉
    */
    public class MultiDownload {
        static int threadCount = 3;
        static int finishedThread = 0;
        //这个地址对吗
        static String path = "http://192.168.3.14:8080/EasyDownload/download.do";
        
        public static void main(String[] args) {
            
            try{
                URL url = new URL(path);
                HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");//get方式可以下载吗?
                conn.setConnectTimeout(5000);
                conn.setReadTimeout(5000);
                if(conn.getResponseCode()==200){
                    int length = conn.getContentLength();
                    File file = new File("a.exe");
                    RandomAccessFile raf = new RandomAccessFile(file,"rwd");
                    raf.setLength(length);
                    raf.close();
                    int size = length/MultiDownload.threadCount;
                    
                    for(int i=0;i<threadCount;i++){
                        int startIndex = i*size;
                        int endIndex = (i+1)*size-1;
                        if(i==threadCount-1){
                            endIndex = length-1;
                        }
                        
                        new DownloadThread(startIndex, endIndex, i).start();
                    }
                    
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
    }
    class DownloadThread extends Thread{
        int startIndex;
        int endIndex;
        int threadId;
        
        public DownloadThread(int startIndex, int endIndex, int threadId) {
            super();
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.threadId = threadId;
        }
        
        @Override
        public void run() {
            int total = 0;
            //再次发送请求,下载
            try{
                File progressFile = new File(threadId+".txt");
                if(progressFile.exists()){//如果存在txt文件
                    FileInputStream fis = new FileInputStream(progressFile);
                    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                    int numberInTxt= Integer.parseInt(br.readLine());
                    startIndex +=numberInTxt;
                    total = numberInTxt;//total必须要在这里给它赋值,不然就不对了
                    fis.close();
                    br.close();
                }
                System.out.println(threadId+"开始:"+startIndex+"结束:"+endIndex);
                URL url = new URL(MultiDownload.path);
                HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");//get方式可以下载吗?
                conn.setConnectTimeout(5000);
                conn.setReadTimeout(5000);
                //看好了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
                if(conn.getResponseCode()==206){
                    InputStream is = conn.getInputStream();
                    byte[] b = new byte[1024];
                    int len = 0;
                    
                    
                    File file = new File("a.exe");
                    RandomAccessFile raf = new RandomAccessFile(file,"rwd");
                    raf.seek(startIndex);//这句很重要!!!!!!!!!!!!!!
                    while((len = is.read(b))!=-1){
                        raf.write(b,0,len);
                        total+=len;
                        if(total%1000 == 0){
                            System.out.println("线程"+threadId+"下载了"+total);
                        }
                        //生成临时文件
                        RandomAccessFile progressRaf = new RandomAccessFile(progressFile,"rwd");
                        progressRaf.write((""+total).getBytes());
                        progressRaf.close();
                    }
                    System.out.println("线程"+threadId+"下载完毕--------------");
                    raf.close();
                    MultiDownload.finishedThread++;
                    
                    synchronized (MultiDownload.path) {
                        if(MultiDownload.finishedThread == MultiDownload.threadCount){
                            for(int i=0;i<MultiDownload.threadCount;i++){
                                File f = new File(i+".txt");
                                f.delete();
                            }
                            MultiDownload.finishedThread = 0;
                        }
                    }
                    
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            
        }
    }
  • 相关阅读:
    File类总结
    MyBatis框架一级缓存与二级缓存
    SpringMVC运行原理总结
    SpringMVC:详述拦截器
    SpringMVC:自定义视图及其执行过程
    详述ThreadLocal
    浅析MVC中的数据流动
    error: gpg failed to sign the data 的一个解决办法
    保险业务核心系统设计参考
    奇怪的404
  • 原文地址:https://www.cnblogs.com/BlogCommunicator/p/7574541.html
Copyright © 2011-2022 走看看