myclass.java~12~

来自「简易多线程下载程序」· JAVA~12~ 代码 · 共 49 行

JAVA~12~
49
字号
package mythead;import java.net.*;import java.io.*;import javax.swing.*;public class myClass extends Thread {  String sURL;  long nStartPos;  long nEndPos;  int i;  public static int theFileLength;  RandomAccessFile dst ;  public static int nowDownLength; public static JProgressBar jProgressBar1;  myClass(String url,RandomAccessFile f,long start, long end,int i,int theFileLength,JProgressBar jProgressBar1) {    this.sURL = url;    this.dst =f;    this.nStartPos = start;    this.nEndPos = end;    this.i=i;    this.theFileLength=theFileLength;    this.jProgressBar1=jProgressBar1;  }  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);        myClass.jProgressBar1.setValue(myClass.nowDownLength/myClass.theFileLength*100);      }    }    catch (Exception ex) {      ex.printStackTrace();    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?