您好,登錄后才能下訂單哦!
使用Java怎么實(shí)現(xiàn)一個多線程斷點(diǎn)下載?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
JAVA多線程斷點(diǎn)下載原理如圖:
代碼如下:
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class MutileThreadDownload { /** * 線程的數(shù)量 */ private static int threadCount = 3; /** * 每個下載區(qū)塊的大小 */ private static long blocksize; /** * 正在運(yùn)行的線程的數(shù)量 */ private static int runningThreadCount; /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // 服務(wù)器文件的路徑 String path = "http://192.168.1.100:8080/ff.exe"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (code == 200) { long size = conn.getContentLength();// 得到服務(wù)端返回的文件的大小 System.out.println("服務(wù)器文件的大?。?quot; + size); blocksize = size / threadCount; // 1.首先在本地創(chuàng)建一個大小跟服務(wù)器一模一樣的空白文件。 File file = new File("temp.exe"); RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.setLength(size); // 2.開啟若干個子線程分別去下載對應(yīng)的資源。 runningThreadCount = threadCount; for (int i = 1; i <= threadCount; i++) { long startIndex = (i - 1) * blocksize; long endIndex = i * blocksize - 1; if (i == threadCount) { // 最后一個線程 endIndex = size - 1; } System.out.println("開啟線程:" + i + "下載的位置:" + startIndex + "~" + endIndex); new DownloadThread(path, i, startIndex, endIndex).start(); } } conn.disconnect(); } private static class DownloadThread extends Thread { private int threadId; private long startIndex; private long endIndex; private String path; public DownloadThread(String path, int threadId, long startIndex, long endIndex) { this.path = path; this.threadId = threadId; this.startIndex = startIndex; this.endIndex = endIndex; } @Override public void run() { try { // 當(dāng)前線程下載的總大小 int total = 0; File positionFile = new File(threadId + ".txt"); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); // 接著從上一次的位置繼續(xù)下載數(shù)據(jù) if (positionFile.exists() && positionFile.length() > 0) {// 判斷是否有記錄 FileInputStream fis = new FileInputStream(positionFile); BufferedReader br = new BufferedReader( new InputStreamReader(fis)); // 獲取當(dāng)前線程上次下載的總大小是多少 String lasttotalstr = br.readLine(); int lastTotal = Integer.valueOf(lasttotalstr); System.out.println("上次線程" + threadId + "下載的總大?。?quot; + lastTotal); startIndex += lastTotal; total += lastTotal;// 加上上次下載的總大小。 fis.close(); } conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); System.out.println("code=" + code); InputStream is = conn.getInputStream(); File file = new File("temp.exe"); RandomAccessFile raf = new RandomAccessFile(file, "rw"); // 指定文件開始寫的位置。 raf.seek(startIndex); System.out.println("第" + threadId + "個線程:寫文件的開始位置:" + String.valueOf(startIndex)); int len = 0; byte[] buffer = new byte[512]; while ((len = is.read(buffer)) != -1) { RandomAccessFile rf = new RandomAccessFile(positionFile, "rwd"); raf.write(buffer, 0, len); total += len; rf.write(String.valueOf(total).getBytes()); rf.close(); } is.close(); raf.close(); } catch (Exception e) { e.printStackTrace(); } finally { // 只有所有的線程都下載完畢后 才可以刪除記錄文件。 synchronized (MutileThreadDownload.class) { System.out.println("線程" + threadId + "下載完畢了"); runningThreadCount--; if (runningThreadCount < 1) { System.out.println("所有的線程都工作完畢了。刪除臨時記錄的文件"); for (int i = 1; i <= threadCount; i++) { File f = new File(i + ".txt"); System.out.println(f.delete()); } } } } } } }
關(guān)于使用Java怎么實(shí)現(xiàn)一個多線程斷點(diǎn)下載問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。