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();
            }
            
        }
    }
  • 相关阅读:
    java内部类_让你一看就明白_再也不用困惑啦
    hibernate中的java对象有几种状态,其相互关系如何(区别和相互转换)
    未曾谋面却完成了短信发送功能_API是个好中介
    日历(Calendar)_java版(某年的日历,某月的日历)_用户完全自定义
    让你的网站拥有微博(weibo.com)关注图标
    一个demo告诉你优化算法的强大
    给你八分钟搞定dedeCMS(织梦内容管理系统)
    SSL协议(HTTPS) 握手、工作流程详解(双向HTTPS流程)
    oracle数据库:jdbc通用sql分页封装
    浅析HTTP协议
  • 原文地址:https://www.cnblogs.com/BlogCommunicator/p/7574541.html
Copyright © 2011-2022 走看看