package com.changfa.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadUtil {
    private String pathFile;
    private String strFile;
    private DownloadThread[] downloadThreadArr;
    private int threadNum;
    private int size;

    public DownloadUtil (String pathFile, String strFile, int threadNum) {
        this.pathFile = pathFile;
        this.threadNum = threadNum;
        downloadThreadArr = new DownloadThread[threadNum];
        this.strFile = strFile;
    }

    public void download() throws Exception {
        URL url = new URL(pathFile);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty(
                "Accept",
                "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
                        + "application/x-shockwave-flash, application/xaml+xml, "
                        + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                        + "application/x-ms-application, application/vnd.ms-excel, "
                        + "application/vnd.ms-powerpoint, application/msword, */*");
        conn.setRequestProperty("Accept-Language", "zh-CN");
        conn.setRequestProperty("Charset", "UTF-8");
        conn.setRequestProperty("Connection", "Keep-Alive");

        size = conn.getContentLength();
        conn.disconnect();
        int currentPartSize = size / threadNum + 1;
        RandomAccessFile file = new RandomAccessFile(strFile, "rw");
        file.setLength(size);
        file.close();
        for (int i = 0; i < threadNum; i++) {
            int start = i * currentPartSize;
            RandomAccessFile currentPart = new RandomAccessFile(strFile, "rw");
            currentPart.seek(start);
            downloadThreadArr[i] = new DownloadThread(start, currentPartSize, currentPart);
            downloadThreadArr[i].start();
        }
    }


    class DownloadThread extends Thread {
        private int start;
        private int size;
        private RandomAccessFile out;
        public int length;

        public DownloadThread(int start, int size,RandomAccessFile out) {
            this.out = out;
            this.start = start;
            this.size = size;
        }

        @Override
        public void run() {
            InputStream is = null;
            try {
                URL url = new URL(pathFile);
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty(
                        "Accept",
                        "image/gif, image/jpeg, image/pjpeg, image/pjpeg, " + "application/x-shockwave-flash, application/xaml+xml, " + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                                + "application/x-ms-application, application/vnd.ms-excel, "
                                + "application/vnd.ms-powerpoint, application/msword, */*");
                conn.setRequestProperty("Accept-Language", "zh-CN");
                conn.setRequestProperty("Charset", "UTF-8");
                is = conn.getInputStream();
                is.skip(this.start);
                byte[] bs = new byte[1024];
                int len = 0;
                while ((len = is.read(bs)) != -1 && length < size) {
                    out.write(bs, 0, len);
                    length += len;
                }
                out.close();
                is.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    out.close();
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }


}







/*
@RestController
@RequestMapping("tc")
public class TestController {

    @RequestMapping("/download")
    public void download(String filePath,String savePath) {
        try {
            DownloadUtil downloadUtil = new DownloadUtil (filePath,savePath,10);
            downloadUtil.download();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}*/
