The principle of downloading JAVA multi-threaded breakpoint is shown in the figure:
The code is as follows:
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class MutileThreadDownload { /** * Number of threads*/ private static int threadCount = 3; /** * Size of each download block*/ private static long blocksize; /** * Number of running threads*/ private static int runningThreadCount; /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // Path of the server file 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();// Get the size of the file returned by the server System.out.println("Server file size: " + size); blocksize = size / threadCount; // 1. First create a blank file with the same size as the server locally. File file = new File("temp.exe"); RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.setLength(size); // 2. Open several child threads to download the corresponding resources separately. runningThreadCount = threadCount; for (int i = 1; i <= threadCount; i++) { long startIndex = (i - 1) * blocksize; long endIndex = i * blocksize - 1; if (i == threadCount) { // The last thread endIndex = size - 1; } System.out.println("Start thread: " + i + "Download location: " + 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 { // The total size of the current thread download int total = 0; File positionFile = new File(threadId + ".txt"); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); // Then continue to download data from the previous location if (positionFile.exists() && positionFile.length() > 0) {// Determine whether there is a record FileInputStream fis = new FileInputStream(positionFile); BufferedReader br = new BufferedReader( new InputStreamReader(fis)); // Get the total size of the current thread lasttotalstr = br.readLine(); int lastTotal = Integer.valueOf(lasttotalstr); System.out.println("Last thread" + threadId + "Total size of download:" + lastTotal); startIndex += lastTotal; total += lastTotal;// Add the total size of the last download. 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"); // Specifies where the file starts writing. raf.seek(startIndex); System.out.println("th" + threadId + "threads: the start position of writing the file: " + 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 { // The record file can be deleted after all threads have been downloaded. synchronized (MutileThreadDownload.class) { System.out.println("Thread" + threadId + "Downloaded"); runningThreadCount--; if (runningThreadCount < 1) { System.out.println("All threads are finished working. Delete files for temporary record"); for (int i = 1; i <= threadCount; i++) { File f = new File(i + ".txt"); System.out.println(f.delete()); } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.