1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        java實(shí)現(xiàn)下載器

        共 7735字,需瀏覽 16分鐘

         ·

        2021-03-18 09:42

        點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”

        優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

          作者 |  一樂樂

        來源 |  urlify.cn/MBra2u

        java實(shí)現(xiàn)下載器(以及創(chuàng)建一個(gè)URL對象)

        1.思路講解:

        (1)注意路徑:是網(wǎng)絡(luò)路徑噢

        (2)創(chuàng)建創(chuàng)建網(wǎng)路協(xié)議對象(遠(yuǎn)程對象):HttpURLConnection urlConnection

        (3)定位網(wǎng)絡(luò)資源:URL url

        (4)調(diào)用url的openConnection()方法,連接遠(yuǎn)程對象

        (5)設(shè)置遠(yuǎn)程對象的參數(shù)和屬性:例如 urlConnection.setRequestProperty("Range", "bytes=0-");

        (6)發(fā)送連接請求,實(shí)現(xiàn)真正的連接:urlConnection.connect();

        (7)通過返回碼,判斷是否連接成功(200以內(nèi)是成功的)

        (8)遠(yuǎn)程對象連接成功后,創(chuàng)建遠(yuǎn)程對象輸入流

        (9)開始讀取與寫入字節(jié)啦

         

        2.代碼:

        package DownLoadDemo;

        import java.io.BufferedInputStream;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.RandomAccessFile;
        import java.net.HttpURLConnection;
        import java.net.URL;
        import java.util.RandomAccess;

        public class DownLoaderTest {
            private static final int MAX_SIZE_BUFF = 1024 * 1024 * 10;     //1MB大小
            //網(wǎng)絡(luò)視頻路徑
            private static String path = "https://www.iqiyi.com/v_uggavqs43w.html";
            private static String writePath = "111.mp4";
            public static void main(String[] args) throws IOException {
                //網(wǎng)絡(luò)協(xié)議對象(遠(yuǎn)程對象)
                HttpURLConnection urlConnection = null;
                //定位網(wǎng)絡(luò)資源
                URL url = new URL(path);
                //調(diào)用url的openConnection()連接遠(yuǎn)程對象
                urlConnection = (HttpURLConnection) url.openConnection();
                //設(shè)置遠(yuǎn)程對象的參數(shù)和屬性:"Range" : 范圍," 0-":沒有終止字節(jié)數(shù),支持?jǐn)帱c(diǎn)續(xù)傳 
                urlConnection.setRequestProperty("Range""bytes=0-");
                //發(fā)送連接請求,實(shí)現(xiàn)真正的連接
                urlConnection.connect();
                //通過返回碼,判斷是否連接成功(200以內(nèi)是成功的)
                int code = urlConnection.getResponseCode();
                if(code / 100 != 2) {    //請求失敗
                    throw new RuntimeException("請求失敗,返回碼:" + code + "錯(cuò)誤");
                }
                //遠(yuǎn)程對象連接成功后,創(chuàng)建遠(yuǎn)程對象輸入流
                InputStream inputStream = urlConnection.getInputStream();
                //創(chuàng)建bufferedInputStream
        //        BufferedInputStream bInStream = new BufferedInputStream(inputStream);
        //        //使用RandomAccess工具類
        //        RandomAccessFile randomAccess = new RandomAccessFile(path, "rw");
                //創(chuàng)建緩沖字節(jié)數(shù)組buff
                byte[] buff = null;
                //開始讀寫
                //1、添加(小技術(shù))打印下載進(jìn)度(下載的"每塊字節(jié)"字節(jié)數(shù)/時(shí)間)
                //獲取文件大小
                int fileSize = urlConnection.getContentLength();
                System.out.println(fileSize);
                int downloadedCount = 0;
                while(downloadedCount < fileSize) {
                    //分塊,處理一下最后剩余不足MAX_SIZE_BUFF量
                    if(fileSize - downloadedCount > MAX_SIZE_BUFF) {
                        buff = new byte[MAX_SIZE_BUFF];
                    }else {
                        buff = new byte[fileSize - downloadedCount];
                    }
                    
                    int currCount = 0;
                    int read = -1;
                    long startTime = System.currentTimeMillis();            
        //            //讀取
        //            bInStream.read(buff);
                    while(currCount < buff.length) {
                        read = inputStream.read();
                        if(read == -1) {
                            break;
                        }
                        buff[currCount++] = (byte)read;
                    }
                    if(read == -1) {
                        break;
                    }
                    long endTime = System.currentTimeMillis();
                    double speed = currCount / ((endTime - startTime) / 1000.0);    //速度b/s
                    System.out.println("讀取速度:" + speed);
                    downloadedCount += currCount;
                }
            
            }
        }


        3.網(wǎng)絡(luò)編程知識: 

        1.URL對象

        URL:統(tǒng)一資源定位符

        作用:

        ☆通過 URL 可以獲得互聯(lián)網(wǎng)資源相關(guān)信息,包括獲得URL所引用的遠(yuǎn)程對象URLConnection,以及URL的輸入流對象InputStream。

        ☆類 URL 是指向互聯(lián)網(wǎng)“資源”的指針。資源可以是簡單的文件或目錄,也可以是對更為復(fù)雜的對象的引用,例如對數(shù)據(jù)庫或搜索引擎的查詢。

        簡單說,就是在網(wǎng)絡(luò)上找到資源的位置(統(tǒng)一資源定位符),才可以通過網(wǎng)絡(luò)的服務(wù)進(jìn)行資源的各種操作:如存取、更新、替換和查找其屬性。(得URL所引用的遠(yuǎn)程對象URLConnection,以及URL的輸入流對象InputStream。)

         

        2.創(chuàng)建一個(gè)URL對象步驟:

        1. 創(chuàng)建URL對象,并通過調(diào)用openConnection方法獲得URLConnection對象;

        2. 設(shè)置URLConnection參數(shù)和普通請求屬性;

        3. 向遠(yuǎn)程資源發(fā)送請求;

        4. 遠(yuǎn)程資源變?yōu)榭捎?,程序可以訪問遠(yuǎn)程資源的頭字段和通過輸入流來讀取遠(yuǎn)程資源返回的信息。

         

         

        3.連接后返回碼含義:

         




        粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

        ??????

        ??長按上方微信二維碼 2 秒


        感謝點(diǎn)贊支持下哈 

        瀏覽 106
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            久久久久区| 久久伊99综合婷婷久久伊 | 韩国三级无码无遮床戏视频 | 国产超碰人人做人人爽aⅴ | 国产无码久 | 91三级大片视频 | 东方a在线 | 大鸡巴在线观看 | 2014亚洲天堂 | 偷拍视频一区 |