Java 다중 스레드 브레이크 포인트를 다운로드하는 원리는 그림에 나와 있습니다.
코드는 다음과 같습니다.
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; 공개 클래스 mutileTheRedDownload { / *** 스레드 수* / private static int strandCount = 3; / *** 각 다운로드 블록의 크기*/ 개인 정적 긴 블록 크기; / *** 실행 스레드 수*/ 개인 정적 int RunningThreadCount; /** * @param args * @Throws Exception */public static void main (String [] args)은 예외를 던지고 서버 파일의 경로 스트링 경로 = "http://192.168.100:8080/ff.exe"; URL URL = 새 URL (PATH); httpurlconnection conn = (httpurlConnection) url.openConnection (); Conn.setRequestMethod ( "get"); Conn.setConnectTimeout (5000); int code = conn.getResponseCode (); if (code == 200) {long size = conn.getContentLength (); // 서버 시스템에서 반환 한 파일의 크기를 가져옵니다. blocksize = 크기 / 스레드 카운트; // 1. 먼저 서버와 동일한 크기의 빈 파일을 로컬로 만듭니다. 파일 = 새 파일 ( "temp.exe"); randomaccessfile raf = 새로운 randomaccessfile (파일, "rw"); raf.setlength (크기); // 2. 해당 리소스를 별도로 다운로드하려면 여러 자식 스레드를 열어줍니다. 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); 새 downloadthread (path, i, startIndex, endIndex) .start (); }} conn.disconnect (); } private static class downloadThread는 스레드 {private int threadid; 비공개 Long StartIndex; 비공개 긴 endindex; 개인 문자열 경로; public downloadthread (문자열 path, int strandid, long startIndex, long endindex) {this.path = path; this.threadid = threadid; this.startIndex = startIndex; this.endindex = endindex; } @override public void run () {try {// 현재 스레드의 총 크기 int total = 0; File PositionFile = 새 파일 (ThreadId + ".txt"); URL URL = 새 URL (PATH); httpurlconnection conn = (httpurlConnection) url .openConnection (); Conn.setRequestMethod ( "get"); // 그런 다음 이전 위치에서 데이터를 계속 다운로드합니다. bufferedReader br = new bufferedReader (new inputStreamReader (FIS)); // 현재 스레드의 총 크기를 가져옵니다. lastTotalStr = br.readline (); int lasttotal = integer.valueof (lastTotalStr); System.out.println ( "마지막 스레드" + 스레드 + "다운로드의 총 크기 :" + lasttotal); startIndex += lasttotal; Total += lasttotal; // 마지막 다운로드의 총 크기를 추가하십시오. fis.close (); } conn.setRequestProperty ( "범위", "bytes =" + startIndex + "-" + endIndex); Conn.setConnectTimeout (5000); int code = conn.getResponseCode (); System.out.println ( "code =" + code); inputStream은 = conn.getInputStream (); 파일 = 새 파일 ( "temp.exe"); randomaccessfile raf = 새로운 randomaccessfile (파일, "rw"); // 파일이 쓰기 시작 위치를 지정합니다. raf.seek (startIndex); System.out.println ( "th" + threadid + "스레드 : 파일 쓰기 시작 위치 :" + string.valueof (startIndex)); int len = 0; 바이트 [] 버퍼 = 새로운 바이트 [512]; while ((len = is.read (buffer))! = -1) {randomaccessfile rf = new randomaccessfile (positionfile, "rwd"); raf.write (Buffer, 0, Len); 총 += 렌; rf.write (String.valueof (Total) .getBytes ()); rf.close (); } is.close (); raf.close (); } catch (예외 e) {e.printstacktrace (); } 마침내 {// 모든 스레드가 다운로드 된 후 레코드 파일을 삭제할 수 있습니다. synchronized (mutileTheRDownload.class) {System.out.println ( "스레드" + 스레드 + "다운로드"); RunningThreadCount--; if (runningthreadCount <1) {System.out.println ( "모든 스레드가 작동 중입니다. 임시 레코드를 위해 파일 삭제"); for (int i = 1; i <= threadCount; i ++) {file f = 새 파일 (i+".txt"); System.out.println (f.delete ()); }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.