myclass.java~10~
来自「简易多线程下载程序」· JAVA~10~ 代码 · 共 45 行
JAVA~10~
45 行
package mythead;import java.net.*;import java.io.*;public class myClass extends Thread { String sURL; long nStartPos; long nEndPos; int i; int theFileLength; RandomAccessFile dst ; public static long nowDownLength; myClass(String url,RandomAccessFile f,long start, long end,int i,int theFileLength) { this.sURL = url; this.dst =f; this.nStartPos = start; this.nEndPos = end; this.i=i; this.theFileLength=theFileLength; } public void run() { try { URL url = new URL(sURL); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setRequestProperty("RANGE", "bytes=" + nStartPos + "-"); InputStream input = httpConnection.getInputStream(); byte[] b = new byte[1024]; int nRead; this.currentThread().setName(""+this.i); while ( (nRead = input.read(b, 0, 1024)) > 0 && nStartPos < nEndPos) { dst.seek(nStartPos); dst.write(b, 0, nRead); myClass.nowDownLength=myClass.nowDownLength+nRead; nStartPos = nStartPos + nRead; System.out.println(""+this.currentThread().getName()+myClass.nowDownLength); } } catch (Exception ex) { ex.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?