麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

java 斷點下載

2019-11-11 06:56:50
字體:
來源:轉載
供稿:網(wǎng)友
package com.bp.util.all.downLoadAndUpload;import com.alibaba.fastjson.JSON;import org.apache.http.Header;import org.apache.http.HttPResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.io.*;import java.util.Date;/** * 我自己寫的一個斷點下載 * * @author current_bp * @createTime 20170116 */public class HttpDownloader1 {    private long fileSize = 0;    private int threadNum = 0;    private int threadMaxNum = 5;    private int cacheMax = 500 * 1024;//每個文件的大小500Kb    private static int[] progress;//0:未開始,1:下載完成,2:下載中,3:寫入完成,4:寫入中    private String fileName = "tomcat";//文件名稱    //    private String requestUrl = "http://mirrors.cnnic.cn/apache/tomcat/tomcat-9/v9.0.0.M17/bin/apache-tomcat-9.0.0.M17.zip";//請求地址    private String baseTMPFilePath = "E://tmp//20170203//";//臨時文件的路徑    //http://localhost:8080/uploadController/download?id=1    //http://mirrors.cnnic.cn/apache/tomcat/tomcat-9/v9.0.0.M17/bin/apache-tomcat-9.0.0.M17.zip    private String requestUrl = "http://mirrors.cnnic.cn/apache/tomcat/tomcat-9/v9.0.0.M17/bin/apache-tomcat-9.0.0.M17.zip";    private String oldFileName = "";    private String tail = "";    private String tmpTail = ".tmp";    /*    1、下載一個文件,    2、將一個文件分割成多個請求獲取,    3、生成多個文件,    4、按照循序組裝文件    5、將寫入完成的文件刪除     */    public static void main(String[] args) {        HttpDownloader1 httpDownloader1 = new HttpDownloader1();//        httpDownloader1.initProgress(10);//        System.out.println("----:" + httpDownloader1.canDownloadWithThread());//        System.out.println("----:fileSize:" + httpDownloader1.getDownloadFileSize());//        httpDownloader1.uSEOneThreadDownloadFile();        long time1 = new Date().getTime();        httpDownloader1.useMoreThreadDownloadFile();        long time2 = new Date().getTime();        System.out.println("===>used time:" + (time2 - time1));    }    /**     * 初始化分片     *     * @param fileAllSize 文件總大小     * @return 分片     */    private int[] initProgress(long fileAllSize) {        //分片的數(shù)量        long len = fileAllSize / cacheMax;        if (0 != fileAllSize % cacheMax) {            len++;        }        System.out.println("===>initProgress: len:" + len);        //線程的數(shù)量        if (len <= this.threadMaxNum) {            this.threadNum = (int) len;        } else {            this.threadNum = this.threadMaxNum;        }        //初始化名稱等其他屬性        this.oldFileName = getOldFileName();        this.tail = getTail();        return new int[(int) len];    }    /**     * 獲取下載的文件的后綴     *     * @return 后綴     */    private String getTail() {        String name = this.oldFileName;        String t = name.substring(name.lastIndexOf("."));        System.out.println("tail:" + t);        return t;    }    /**     * 根據(jù)URL獲取文件名稱     *     * @return 文件名稱     */    private String getOldFileName() {        String url = this.requestUrl;        String name = url.substring(url.lastIndexOf("/") + 1);        System.out.println("name:" + name);        return name;    }    /**     * 獲取下一個需要下載的文件分片     *     * @return 大于0:需要下載,小于0:沒有需要下載的了     */    private synchronized int getNextRequestProgress() {        int result = -1;        for (int i = 0; i < progress.length; i++) {            if (progress[i] == ProgressStatus.NO_START.getKey()) {                result = i;                progress[i] = ProgressStatus.DOWNLOAD_ING.getKey();                break;            }        }        return result;    }    /**     * 修改分片的狀態(tài)     *     * @param index     分片位置     * @param newStatus 新狀態(tài)     * @param oldStatus 舊狀態(tài)     * @return 是否成功     */    private synchronized boolean changeProgress(int index, int newStatus, int oldStatus) {        int oldStatus1 = progress[index];        System.out.println("===>changeProgress: index:" + index + " newStatus:" + newStatus + " oldStatus:" + oldStatus + " oldStatus1:" + oldStatus1);        //0:未開始,1:下載完成,2:下載中,3:寫入完成        //原狀態(tài)不能吻合,錯誤        if (oldStatus != oldStatus1) {            return false;        }        progress[index] = newStatus;        return true;    }    /**     * 判斷是否能夠使用多線程下載     *     * @return 能否使用多線程下載     */    public boolean canDownloadWithThread() {        CloseableHttpClient httpclient = HttpClients.createDefault();        HttpGet httpget = new HttpGet(requestUrl);        httpget.addHeader("Range", "bytes=" + 0 + "-" + 99);        HttpResponse response;        String result;        try {            response = httpclient.execute(httpget);            int statusCode = response.getStatusLine().getStatusCode();            result = EntityUtils.toString(response.getEntity());            System.out.println("===>statusCode:" + statusCode);            System.out.println("===>result:" + result);            System.out.println("===>headers:" + JSON.toJSONString(response.getAllHeaders()));            if (206 == statusCode) {                return true;            }        } catch (IOException e) {            e.printStackTrace();        }        return false;    }    /**     * 獲取下載文件大小     *     * @return 文件大小     */    private Long getDownloadFileSize() {        CloseableHttpClient httpclient = HttpClients.createDefault();        HttpGet httpget = new HttpGet(requestUrl);        httpget.addHeader("Range", "bytes=" + 0 + "-" + 99);        HttpResponse response;        Long contentLength = -1L;        try {            response = httpclient.execute(httpget);            int statusCode = response.getStatusLine().getStatusCode();            System.out.println("===>statusCode:" + statusCode);            System.out.println("===>headers:" + JSON.toJSONString(response.getAllHeaders()));            Header[] headers = response.getHeaders("Content-Range");            if (headers.length > 0) {                contentLength = Long.valueOf(headers[0].getValue().split("/")[1]);            }            return contentLength;        } catch (IOException e) {            e.printStackTrace();        }        return -1L;    }    /**     * 將臨時文件拼接起來     *     * @return 是否完成拼接     */    private boolean pickUpFile() {        for (int i = 0; i < progress.length; i++) {            while (true) {                System.out.println("===>progress status:" + JSON.toJSONString(progress));                //如果需要拼接的分片的狀態(tài)不對,就睡眠2秒                if (ProgressStatus.NO_START.getKey() == progress[i] || progress[i] == ProgressStatus.DOWNLOAD_ING.getKey()                        || progress[i] == ProgressStatus.WRITE_ING.getKey()) {                    System.out.println("===>progress :" + i + " progress status:" + ProgressStatus.getValueByKey(progress[i]) +                            " ,and will sleep 2 seconds!!");                    try {                        Thread.sleep(2 * 1000L);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                //需要拼接的狀態(tài)                if (progress[i] == ProgressStatus.DOWNLOAD_OK.getKey()) {                    System.out.println("===>progress :" + i + " progress status:" + ProgressStatus.getValueByKey(progress[i]));                    changeProgress(i, ProgressStatus.WRITE_ING.getKey(), ProgressStatus.DOWNLOAD_OK.getKey());                    InputStream inputStream = null;                    OutputStream outputStream = null;                    try {                        File outputFile = new File(this.baseTMPFilePath + this.fileName + tail);                        if (!outputFile.exists()) {                            outputFile.createNewFile();                        }                        inputStream = new FileInputStream(new File(this.baseTMPFilePath + this.fileName + "_" + i + tmpTail));                        outputStream = new FileOutputStream(outputFile, true);                        byte[] cache = new byte[1024];                        while (-1 != (inputStream.read(cache))) {                            outputStream.write(cache);                        }                    } catch (Exception e) {                        System.out.println("===>pickUpFile: error:" + e.getMessage());                    } finally {                        //關閉資源                        if (null != outputStream) {                            try {                                outputStream.flush();                                outputStream.close();                            } catch (IOException e) {                                e.printStackTrace();                            }                        }                        if (null != inputStream) {                            try {                                inputStream.close();                            } catch (IOException e) {                                e.printStackTrace();                            }                        }                    }                }                //修改分片的狀態(tài),并刪除已經(jīng)追加成功的臨時文件                changeProgress(i, ProgressStatus.WRITE_OK.getKey(), ProgressStatus.WRITE_ING.getKey());                File rmFile = new File(this.baseTMPFilePath + this.fileName + "_" + i + ".tmp");                rmFile.delete();                //完成本次的輸入                break;            }        }        return false;    }    /**     * 使用一個線程下載     */    public void useOneThreadDownloadFile() {        System.out.println("===>start download .....");        DownLoadThread downLoadThread = new DownLoadThread(0, getDownloadFileSize(), 0);        downLoadThread.run();    }    /**     * 用更多的線程下載     */    public void useMoreThreadDownloadFile() {        System.out.println("===>start download .....");        fileSize = getDownloadFileSize();        //將一個文件分片,劃分線程數(shù)        progress = initProgress(fileSize);        //this.threadNum        for (int i = 0; i < this.threadNum; i++) {            System.out.println("===>啟動線程:threadNum:" + (i + 1) + " .....");            DownLoadThread downLoadThread = new DownLoadThread();            downLoadThread.start();        }        pickUpFile();    }    /**     * 專門用于下載的線程     */    class DownLoadThread extends Thread {        private long start = 0;        private long end = 0;        private int whichProgress = 0;//需要下載的是哪個分片        public DownLoadThread() {        }        public DownLoadThread(long start, long end, int whichProgress) {            this.start = start;            this.end = end;//            this.end = 37238L;            this.whichProgress = whichProgress;        }        @Override        public void run() {            //循環(huán)下載===>開始            while (true) {                //獲取需要下載的分片                this.whichProgress = getNextRequestProgress();                if (this.whichProgress == -1) {                    System.out.println("===>thread: id:" + Thread.currentThread().getId() +                            " this progress is end!!! progress:" + this.whichProgress);                    return;                }                this.start = this.whichProgress * cacheMax;                this.end = (this.whichProgress + 1) * cacheMax - 1;                //下載每一個分片的內(nèi)容                System.out.println("===>thread: id:" + Thread.currentThread().getId() +                        "下載的分片:開始:" + start + "結束:" + end + " 分片:" + whichProgress);                BufferedInputStream inputStream ;                FileOutputStream fileOutputStream = null;                //.jpg                File tmpFile = new File(baseTMPFilePath + fileName + "_" + whichProgress + tmpTail);                CloseableHttpClient httpclient = HttpClients.createDefault();                HttpGet httpget = new HttpGet(requestUrl);                httpget.addHeader("Range", "bytes=" + start + "-" + end);                try {                    HttpResponse response = httpclient.execute(httpget);                    inputStream = new BufferedInputStream(response.getEntity().getContent());                    fileOutputStream = new FileOutputStream(tmpFile);//                response.getEntity().writeTo(fileOutputStream);                    byte[] cache = new byte[1024];                    int size = 0;                    while (-1 != (size = inputStream.read(cache))) {                        fileOutputStream.write(cache, 0, size);                    }                    System.out.println("===>thread: id: " + Thread.currentThread().getId() +                            " download 分片:" + whichProgress + " is over!!!");                    changeProgress(whichProgress, ProgressStatus.DOWNLOAD_OK.getKey(), ProgressStatus.DOWNLOAD_ING.getKey());                } catch (IOException e) {                    e.printStackTrace();                } finally {                    try {//                    fileOutputStream.flush();                        fileOutputStream.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }            }            //循環(huán)下載===>結束        }    }    /**     * 關于下載過程中的進程狀態(tài)     */    enum ProgressStatus {        //0:未開始,1:下載完成,2:下載中,3:寫入完成,4:寫入中,5:        NO_START(0, "未開始"),        DOWNLOAD_OK(1, "下載完成"),        DOWNLOAD_ING(2, "下載中"),        WRITE_OK(3, "寫入完成"),        WRITE_ING(4, "寫入中");        private String value;        private int key;        private ProgressStatus(int key, String value) {            this.key = key;            this.value = value;        }        public static String getValueByKey(int key) {            for (ProgressStatus progressStatus : ProgressStatus.values()) {                if (progressStatus.key == key) {                    return progressStatus.getValue();                }            }            return null;        }        public String getValue() {            return value;        }        public void setValue(String value) {            this.value = value;        }        public int getKey() {            return key;        }        public void setKey(int key) {            this.key = key;        }    }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩精品久久久 | 草碰人人| 一级毛片在线观看视频 | 国产九色视频在线观看 | 主播粉嫩国产在线精品 | 日韩精品中文字幕一区二区三区 | 国产免费一区二区三区网站免费 | 国产一区视频在线观看免费 | 日韩视频在线不卡 | 国产一区精品视频 | 天天舔夜夜操 | 欧美成人免费电影 | 在线免费av观看 | www久久综合 | 日本精品免费观看 | 羞羞视频在线免费 | 香蕉成人在线视频 | 男女一边摸一边做羞羞视频免费 | 午夜视频福利 | av在线免费看网址 | 国产精品久久久久久久久久了 | 一级黄片毛片免费看 | 日本高清黄色片 | 黄色片网站在线免费观看 | 国产一级淫 | 成人午夜激情视频 | 国产精品视频一区二区三区四 | va免费视频 | 一级黄色淫片 | 久久sp| 乱淫67194| 国产99一区二区 | 亚洲欧美一区二区三区在线观看 | 一级大黄毛片 | 欧美大荫蒂xxx | 久久精品免费网站 | 水多视频在线观看 | 国产亚洲精品久久久久久久 | 毛片在线免费播放 | 久久精品视频网址 | 日韩视频在线观看免费视频 |