標籤:nis http 請求 trace request seek 多線程 get for
原文:http://www.open-open.com/lib/view/open1423214229232.html
其實多線程斷點下載原理,很簡單的,那麼我們就來先瞭解下,如何?多線程的斷點下載,首先:你必須明白第一點,那麼就是,什麼是多線程下載,該知識點可以查看本部落格上一篇文章,Android之多線程下載原理,斷點下載呢,其實就是在這個的基礎之上添加了一些東西,那麼添加了什麼東西了,現在來做一個詳細的瞭解。
1.在下載的過程中,邊下載,變用一個檔案來記錄下載的位置,也就是下載了多少的資料
1.建立檔案
2.記錄下載多少資料
3.儲存資料
2.第二次下載的時候,就去讀取檔案中是否存有資料,讀取上次下載的位置,作為這次開始下載的位置
1.建立檔案對象
2.檢驗是否有次檔案和檔案裡面是否有資料
3.讀取資料,將資料拿給這次的開始位置,也就是從這個資料這裡開始下載
3.檔案下載完成之後,將記錄的檔案刪除,一定要下載完成之後,在將檔案刪除,不然會跳出一些異常,比如,這次檔案沒了,就要重新開始下載等等
4.以上說了這些是不是稍微明白了些,那麼下面來看看真正的實踐吧
樣本源碼:
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; /** * 檔案下載器 * * @author Administrator zengtao * */ public class DemoLoader { private static DemoLoader loader = new DemoLoader(); private static int threadCount = 3; private static int runningThread = 3; private DemoLoader() { } public static DemoLoader getInstance() { return loader; } /** * 去伺服器端下載檔案 * * @param path * 伺服器位址 */ public void downFile(String path) { // 去伺服器端擷取檔案的長度,在本地建立一個跟伺服器一樣大小的檔案 try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); int code = connection.getResponseCode(); if (code == 200) { // 1.擷取伺服器端檔案的長度 int fileLength = connection.getContentLength(); // 2.本地建立一個跟伺服器一樣大小的檔案 RandomAccessFile raf = new RandomAccessFile("setup.exe", "rwd"); raf.setLength(fileLength); raf.close(); // 3.假設三個線程下載 int blockSize = fileLength / threadCount; for (int threadId = 0; threadId < threadCount; threadId++) { int startIndex = (threadId - 1) * blockSize; int endIndex = threadId * blockSize - 1; if (threadId == threadCount) { endIndex = fileLength; } // log 假設下載 System.out.println("假設線程:" + threadId + ",下載:" + startIndex + "--->" + endIndex); // 4.開始下載 new DownLoadThread(threadId, startIndex, endIndex, path) .start(); } System.out.println("檔案總長度為:" + fileLength); } else { System.out.println("請求失敗!"); } } catch (Exception e) { e.printStackTrace(); } } /** * 下載檔案的線程 * * @author Administrator zengtao * */ public class DownLoadThread extends Thread { private int threadId; private int startIndex; private int endIndex; private String path; /** * * @param threadId * 線程id * @param startIndex * 線程下載開始位置 * @param endIndex * 線程下載結束位置 * @param path * 線程下載結束檔案放置地址 */ public DownLoadThread(int threadId, int startIndex, int endIndex, String path) { super(); this.threadId = threadId; this.startIndex = startIndex; this.endIndex = endIndex; this.path = path; } @Override public void run() { super.run(); try { // 1.檢驗是否有存的記錄 ------------------------------------------------------------------------------------------------- File file = new File(threadId + ".txt"); if (file.exists() && file.length() > 0) { FileInputStream fis = new FileInputStream(file); byte[] temp = new byte[1024]; int leng = fis.read(temp); String loadLength = new String(temp, 0, leng); int load = Integer.parseInt(loadLength); startIndex = load; fis.close(); } URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // 2.請求伺服器下載部分的檔案,制定開始的位置,和結束位置 connection.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); // log 真實下載 System.out.println("真實線程:" + threadId + ",下載:" + startIndex + "--->" + endIndex); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); // 3.從伺服器擷取的全部資料,返回:200,從伺服器擷取部分資料,返回:206 int code = connection.getResponseCode(); System.out.println("code = " + code); InputStream is = connection.getInputStream(); RandomAccessFile raf = new RandomAccessFile("setup.exe", "rwd"); raf.seek(startIndex); // 隨機寫檔案的時候,從什麼時候開始 int len = 0; int total = 0; // 記錄下載多少 ----------------------------------------- byte[] buff = new byte[1024]; while ((len = is.read(buff)) != -1) { RandomAccessFile info = new RandomAccessFile(threadId + ".txt", "rwd"); raf.write(buff, 0, len); total += len; info.write(("" + startIndex + total).getBytes()); // 4.存資料:(真正下載到開始的位置)下載的+開始的---------------------------------------- info.close(); } is.close(); raf.close(); System.out.println("線程:" + threadId + ",下載完成"); } catch (Exception e) { e.printStackTrace(); } finally { // 5.notice一定要檔案都下載完畢之後再將記錄檔案刪除 runningThread--; if (runningThread == 0) { for (int i = 1; i <= threadCount; i++) { File file = new File(i + ".txt"); file.delete(); } System.out.println("檔案下載完畢,刪除記錄檔案"); --------------------------------------------------------------------- } } } } }
java多線程斷點下載原理(代碼執行個體示範)