Javaマルチスレッドブレークポイントをダウンロードする原則を図に示します。
コードは次のとおりです。
java.io.bufferedreaderをインポートします。 java.io.fileをインポートします。 java.io.fileinputStreamをインポートします。 java.io.inputStreamReaderをインポートします。 java.io.randomaccessfileをインポートします。 java.net.httpurlconnectionをインポートします。 java.net.urlをインポートします。 public class mutilethreaddownload { / ***スレッド数* / private static int threadcount = 3; / ***各ダウンロードブロックのサイズ*/ private static long blocksize; / ***実行中のスレッドの数*/ private static int runningThreadCount; /** * @param args * @throws Exception */public static void main(string [] args)throws exception {// serverファイルのパス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(); //サーバーsystem.out.println( "サーバーファイルサイズ:" +サイズ)によって返されたファイルのサイズを取得します。 blocksize = size / threadcount; //1。最初に、サーバーと同じサイズの空白のファイルをローカルに作成します。 file file = new file( "temp.exe"); randomAccessfile raf = new RandomAccessFile(file、 "rw"); raf.setlength(size); //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( "start thread:" + i + "ダウンロード場所:" + startindex + "〜" + endindex); new downloadThread(path、i、startindex、endindex).start(); }} conn.disconnect(); } private static class downloadthread extends thread {private int threadid;プライベートLong StartIndex; Private long Endindex;プライベートストリングパス。 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 {//現在のスレッドの合計サイズダウンロードint total = 0; file positionfile = new file(threadid + ".txt"); url url = new url(path); httpurlconnection conn =(httpurlconnection)url .openconnection(); conn.setRequestMethod( "get"); //前の場所からデータを続行し続けます(positionfile.exists()&& positionfile.length()> 0){//レコードfileinputStream fis = new fileinputStream(positionFile); BufferedReader BR = new BufferedReader(new inputStreamReader(FIS)); //現在のスレッドの合計サイズを取得しますlastTotalStr = br.readline(); int lasttotal = integer.valueof(lasttotalstr); System.out.println( "last thread" + threadid + "ダウンロードの合計サイズ:" + 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( "th" + 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);合計 += len; rf.write(string.valueof(total).getBytes()); rf.close(); } is.close(); raf.close(); } catch(Exception e){e.printstacktrace(); }最後に{//すべてのスレッドがダウンロードされた後、レコードファイルを削除できます。 Synchronized(mutilethreaddownload.class){system.out.println( "thread" + threadid + "downloaded"); 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()); }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}ティー。 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。