Multi-thread breakpoint download in Java and multi-thread breakpoint download in java
JAVA multi-thread breakpoint download Principle
The Code is as follows:
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 {/*** Number of threads */private static int threadCount = 3;/*** size of each download block */private static long blocksize; /*** Number of running threads */private s Tatic int runningThreadCount;/*** @ param args * @ throws Exception */public static void main (String [] args) throws Exception {// server file path 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) {lon G 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 in the same size as the server. File file = new File ("temp.exe"); RandomAccessFile raf = new RandomAccessFile (file, "rw"); raf. setLength (size); // 2. start several sub-threads to download corresponding resources respectively. 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 ("enable 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; t His. endIndex = endIndex ;}@ Override public void run () {try {// total size of the current thread downloaded int total = 0; File positionFile = new File (threadId + ". txt "); URL url = new URL (path); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setRequestMethod ("GET"); // continue to download data from the previous position if (positionFile. exists () & positionFile. length ()> 0) {// determine whether there is a record of FileInputStream fiis = new FileInputStream (positio NFile); BufferedReader br = new BufferedReader (new InputStreamReader (FCM); // gets the total size of the last downloaded String lasttotalstr = br. readLine (); int lastTotal = Integer. valueOf (lasttotalstr); System. out. println ("last thread" + threadId + "total download size:" + lastTotal); startIndex + = lastTotal; total + = lastTotal; // Add the total size of the last download. Fiis. 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"); // specify the start position of the File. Raf. seek (startIndex); System. out. println ("Number" + threadId + "thread: Start position of the file to be written:" + 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 only 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. Delete the temporary record File "); 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 for your learning and support for helping customers.