JavaSE-多线程下载

package top.mengmei219;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownLoad {
    
    static int threadCound = 3; //线程数
    static int size; //每个线程分的大小
    static int finishThread = 0; //活动的线程数

    //多线程断点续传 下载
    public static void main(String[] args) {
        try {
            URL url = new URL("http://192.168.1.104:8080/itheima74/lczd.pdf");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            int contentLength = urlConnection.getContentLength();
            
            RandomAccessFile random = new RandomAccessFile("/Users/mengmei/Desktop/lang.pdf", "rw");
            random.setLength(contentLength);
            
            for(int threadID=0; threadID<threadCound; threadID++){
                size = contentLength/threadCound;
                int startPosition = threadID*size;
                int endPosition = (threadID+1)*size-1;
                if(threadID == threadCound-1){
                    endPosition = contentLength-1;
                }
                new MyThread(threadID, startPosition, endPosition).start();
            }
            
            
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    
    static class MyThread extends Thread{
        
        private int threadID;
        private int startPosition;
        private int endPosition;

        public MyThread(int threadID, int startPosition, int endPosition) {
            this.threadID = threadID;
            this.startPosition = startPosition;
            this.endPosition = endPosition;
            System.out.println("threadID"+threadID+": "+startPosition+" - "+endPosition);
        }

        @Override
        public void run() {
            int lastPosition = startPosition;
            
            try {
                URL url = new URL("http://192.168.1.104:8080/itheima74/lczd.pdf");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setConnectTimeout(1000*10);
                
                File tempFile = new File("/Users/mengmei/Desktop/"+threadID+".txt");
                if(tempFile.exists()){
                    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile)));
                    String line = reader.readLine();
                    lastPosition = Integer.parseInt(line);
                    startPosition = lastPosition;
                    
                    reader.close();
                }
                
                //分段请求头
                urlConnection.setRequestProperty("Range", "bytes="+startPosition+"-"+endPosition);
                
                int code = urlConnection.getResponseCode();
                if (code == 206) { //分段请求成功
                    InputStream inputStream = urlConnection.getInputStream();
                    byte[] buffer = new byte[1024*10];
                    int len = 0;
                    
                    RandomAccessFile random = new RandomAccessFile("/Users/mengmei/Desktop/lang.pdf", "rw");
                    random.seek(startPosition);
                    
                    
                    while ( (len=inputStream.read(buffer)) > 0 ) {
                        random.write(buffer, 0, len);
                        
                        lastPosition = lastPosition + len;
                        FileOutputStream tempOut = new FileOutputStream(tempFile);
                        tempOut.write(String.valueOf(lastPosition).getBytes());
                        tempOut.flush();
                        tempOut.close();
                    }
                    inputStream.close();
                    random.close();
                }
                
                
                synchronized (MyThread.class) {
                    System.out.println(threadID+" - 下载完成!");
                    finishThread ++;
                    
                    if (finishThread == threadCound) {
                        System.out.println("全部下载完成,删掉临时文件!");
                        for(int i=0; i<threadCound; i++){
                            File temp = new File("/Users/mengmei/Desktop/"+threadID+".txt");
                            if (!temp.delete()) {
                                temp.deleteOnExit(); //在JVM退出时删除
                            }
                        }
                    }
                }
                
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
    
    

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容