Prinsip mengunduh breakpoint java multi-threaded ditunjukkan pada gambar:
Kodenya adalah sebagai berikut:
impor java.io.bufferedReader; impor java.io.file; impor java.io.fileInputStream; impor java.io.inputStreamReader; impor java.io.randomaccessfile; impor java.net.httpurlconnection; impor java.net.url; kelas publik mutilethreaddownload { / *** jumlah utas* / private static int threadcount = 3; / *** Ukuran setiap blok unduhan*/ blocksize panjang statis pribadi; / *** Jumlah utas yang berjalan*/ private static int runningThreadCount; /** * @param args * @throws Exception */public static void main (string [] args) melempar Exception {// path file string file server = "http://192.168.1.100:8080/ff.exe"; Url url = url baru (jalur); HttpurlConnection conn = (httpurlconnection) url.openconnection (); Conn.setRequestMethod ("Get"); Conn.SetConnectTimeout (5000); int kode = conn.getResponsecode (); if (code == 200) {ukuran panjang = conn.getContentLength (); // Dapatkan ukuran file yang dikembalikan oleh server system.out.println ("Ukuran file server:" + size); blocksize = size / threadCount; // 1. Pertama buat file kosong dengan ukuran yang sama dengan server secara lokal. File file = file baru ("temp.exe"); RandomAccessFile RAF = RandomAccessFile baru (File, "RW"); raf.setlength (ukuran); // 2. Buka beberapa utas anak untuk mengunduh sumber daya yang sesuai secara terpisah. RunningThreadCount = ThreadCount; untuk (int i = 1; i <= threadCount; i ++) {startIndex long = (i - 1) * blocksize; Long EndIndex = i * Blocksize - 1; if (i == threadCount) {// utas terakhir endIndex = size - 1; } System.out.println ("Mulai utas:" + i + "Unduh lokasi:" + startIndex + "~" + endIndex); unduhan baru (path, i, startIndex, endIndex) .start (); }} conn.disconnect (); } Private Static Class DownloadThread Extends Thread {private int threadId; StartIndex Long Pribadi; endindex panjang pribadi; jalur string pribadi; download publik (jalur string, int threadId, startIndex panjang, endIndex panjang) {this.path = path; this.threadid = threadId; this.startIndex = startIndex; this.endIndex = endIndex; } @Override public void run () {coba {// ukuran total dari utas saat ini unduhan int total = 0; File positionFile = file baru (threadId + ".txt"); Url url = url baru (jalur); HttpurlConnection conn = (httpurlconnection) url .openconnection (); Conn.setRequestMethod ("Get"); // Lalu lanjutkan untuk mengunduh data dari lokasi sebelumnya jika (positionFile.exists () && positionFile.length ()> 0) {// Tentukan apakah ada rekaman fileInputStream fis = FileInputStream baru (PositionFile); BufferedReader BR = BufferedReader baru (InputStreamReader baru (FIS)); // Dapatkan ukuran total utas saat ini lasttotalstr = br.readline (); int lasttotal = integer.valueof (lastTotalStr); System.out.println ("utas terakhir" + threadId + "ukuran total unduhan:" + lasttotal); startIndex += lasttotal; Total += LastTotal; // Tambahkan ukuran total unduhan terakhir. fis.close (); } conn.setRequestProperty ("range", "bytes =" + startIndex + "-" + endIndex); Conn.SetConnectTimeout (5000); int kode = conn.getResponsecode (); System.out.println ("Code =" + Code); InputStream adalah = conn.getInputStream (); File file = file baru ("temp.exe"); RandomAccessFile RAF = RandomAccessFile baru (File, "RW"); // Menentukan di mana file mulai menulis. raf.seek (startIndex); System.out.println ("th" + threadId + "Threads: Posisi awal menulis file:" + string.ValueOf (startIndex)); int len = 0; byte [] buffer = byte baru [512]; while ((len = is.read (buffer))! = -1) {randomAccessFile rf = baru acak AccawessFile (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 (); } akhirnya {// File rekaman dapat dihapus setelah semua utas diunduh. disinkronkan (mutilethreaddownload.class) {system.out.println ("thread" + threadId + "diunduh"); RunningThreadCount--; if (runningThreadCount <1) {System.out.println ("Semua utas selesai berfungsi. Hapus file untuk catatan sementara"); untuk (int i = 1; i <= threadCount; i ++) {file f = file baru (i+".txt"); System.out.println (f.delete ()); }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }}}}}}}}}}}}}}}}}}}}}}}}}}}}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.