先放代码吧~先是主方法的类~package df;import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; 
import java.net.*; 
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JProgressBar;public class Download extends JFrame{
static int size;
long contentLen;
static int bytesRead;
int zongsize=0,zongbytesRead=0;
Label xiazaidizhi,baocundizhi,Treatshu;  
TextField UCLdizhi,baocun,Treatid; 
JButton liulan;        //浏览窗口
Button button1,button2,button3;   
Label sudu,baigenbi,shijian;
TextField suduzhi,shijianzhi;
TextArea xiaoxi;       //消息显示区
JFileChooser fc = new JFileChooser();
JFrame thisjf;
JProgressBar jpr=new JProgressBar();
//ipr.paintString = true;
GridBagConstraints grid;
public Download(int size){
super("下载器");
thisjf=this;
this.size = size;
zongsize += size;
}
public void init(){
xiazaidizhi = new Label("下载地址:");
UCLdizhi = new TextField("",50); 
baocundizhi = new Label("保存地址:");
baocun = new TextField("d:",50);
liulan = new JButton("    浏览    ");
liulan.addActionListener(new ButtonListener1());
Treatshu = new Label("  线程数  :");
Treatid = new TextField("1",50);
button1 = new Button("建立下载任务");    //下载按键
button1.addActionListener(new ButtonListener2());       //监听
sudu = new Label("当前速度");
baigenbi = new Label("下载进度");
shijian = new Label("剩余时间");
suduzhi = new TextField("",8);
shijianzhi = new TextField("",8);
button2 = new Button("暂停");
button1.addActionListener(new ButtonListener3());
button3 = new Button("继续");
button1.addActionListener(new ButtonListener4());

xiaoxi = new TextArea();
xiaoxi.setSize(500, 150);
grid = new GridBagConstraints();
setLayout(new GridBagLayout());    //构建下载器窗口
setSize(550,350);
//grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 0;grid.gridy = 0;
add(xiazaidizhi,grid);
grid.gridx = 1;grid.gridy = 0;
add(UCLdizhi,grid);
grid.gridx = 0;grid.gridy = 1;
add(baocundizhi,grid);
grid.gridx = 1;grid.gridy = 1;
add(baocun,grid);
grid.gridx = 2;grid.gridy = 1;
add(liulan,grid);
grid.gridx = 0;grid.gridy = 2;
add(Treatshu,grid);
grid.gridx = 1;grid.gridy = 2;
add(Treatid,grid);
grid.gridx = 2;grid.gridy = 2;
add(button1,grid);
grid.gridx = 0;grid.gridy = 3;
add(sudu,grid);
grid.gridx = 1;grid.gridy = 3;
add(baigenbi,grid);
grid.gridx = 2;grid.gridy = 3;
add(shijian,grid);
grid.gridx = 0;grid.gridy = 4;
add(suduzhi,grid);
grid.gridx = 1;grid.gridy = 4;
add(jpr,grid);
grid.gridx = 2;grid.gridy = 4;
add(shijianzhi,grid);
grid.gridx = 0;grid.gridy = 5;
add(button2,grid);
grid.gridx = 1;grid.gridy = 5;
add(button3,grid);
grid.gridx = 0;grid.gridy = 6;grid.gridwidth = 3;
add(xiaoxi,grid);
//pack();
setVisible(true);
}
class ButtonListener1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getSource() == liulan) {
            fc.setSelectedFile(new File(UCLdizhi.getText()));
            int intRetVal = fc.showSaveDialog(thisjf);
            if (intRetVal == JFileChooser.APPROVE_OPTION) {
             baocun.setText(fc.getSelectedFile().getPath());
            }
        }
}
}
class ButtonListener2 implements ActionListener{
 public void actionPerformed(ActionEvent e){   //在按了下载按键之后执行
 Date date=new Date();//获取时间
 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");//转换格式
 File f = new File(baocun.getText());           //定义下载到自己电脑的文件 
 try{
  URL url = new URL(UCLdizhi.getText());           //创建URL   
HttpURLConnection aa = (HttpURLConnection) url.openConnection();  //建立连接
contentLen = aa.getContentLength();                    //获得资源长度
jpr.setMaximum((int)contentLen);
long subLen = contentLen/Integer.parseInt(Treatid.getText());     //获取每块的大小
byte[] dt = new byte[Integer.parseInt(Treatid.getText())];
for(int i=0;i<Integer.parseInt(Treatid.getText());i++){          //创建线程
long length = subLen;
    if (i == (Integer.parseInt(Treatid.getText())-1)) {
     length = contentLen - i * subLen;
    }
    DLThread dt1 = new DLThread(url, length, i * subLen,f,xiaoxi);
    dt1.start();
}
jpr.setStringPainted(true);
//jpr.setValue(zongsize);
}catch(Exception e1){ 
//e1.getStackTrace();
//xiaoxi.append("下载失败。"+"\n"+sdf.format(date)+"\n");
}
/*xiaoxi.append(String.valueOf(size)+"\n");
xiaoxi.append(String.valueOf(zongsize)+"\n");*/
//thisjf.repaint();
if(zongsize==contentLen){
xiaoxi.append("下载成功。保存在 "+f+"\n"+sdf.format(date)+"\n");
}
 }
}
class ButtonListener3 implements ActionListener{
public void actionPerformed(ActionEvent e) {
//dt1.interrupt();
}
}
class ButtonListener4 implements ActionListener{
public void actionPerformed(ActionEvent e) {
//dt1.run();
}
}
public static void main(String[] args){ 
Download abc = new Download(size);
abc.init();
}
}

解决方案 »

  1.   

    这个是线程的类~
    package df;import java.awt.Label;
    import java.awt.TextArea;
    import java.awt.TextField;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.RandomAccessFile;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Date;
    import java.text.SimpleDateFormat;import javax.swing.JProgressBar;public class DLThread extends Thread{
    URL url;
    long length;
    long startLen;
    File f;
    TextArea xiaoxi;
    /*TextField suduzhi,baifenbizhi,shijianzhi;
    JProgressBar jpr1;
    Label sudu1;*/
    int size;
    public DLThread(URL url, long length, long startLen, File f, TextArea c) {
       super();
       this.url = url;
       this.length = length;
       this.startLen = startLen;
       this.f = f;
       this.xiaoxi = c;
      // this.jpr1=jpr;
    }
    public void run(){
    Date date=new Date();//获取时间
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
    try {
    HttpURLConnection aa =  (HttpURLConnection) url.openConnection();  //建立连接
        aa.setAllowUserInteraction(true);    //设置此aa的allowUserInteraction字段的值
        
        aa.setRequestProperty("RANGE", "bytes=" + startLen + "-");  //设置获取资源数据的范围,从startLen到最后
        RandomAccessFile randout = new RandomAccessFile(f, "rw");   //创建RandomAccessFile
        randout.seek(startLen);         //设置这个线程开始写入的位置,startLen的下一个位置开始
        
        byte[] buffer = new byte[10240];             //设置缓冲区大小
        int bytesRead;
        size = 0;                                 //该线程下载总大小
        InputStream ins = aa.getInputStream();
        while ((bytesRead = ins.read(buffer)) != -1) {
        size += bytesRead;
        if (size > length) {
         bytesRead = (int) (bytesRead - (size - length));
         size = (int) length;
        }
        Download pro = new Download(size);
        randout.write(buffer, 0, bytesRead);
        //jpr1.setValue(size);
        //xiaoxi.append(String.valueOf(size));
        }
        xiaoxi.append("线程" + this.getId() + "下载" + startLen
              + "到"+(startLen+length)+"\n");
        randout.close();
        }catch (IOException e) {
        e.printStackTrace();
        xiaoxi.append("失败。"+"\n"+sdf.format(date)+"\n");
       }
    }
    }
      

  2.   

    Download pro = new Download(size);
    线程类里面的这个~本来想发是想把这个SIZE每次下载后就传到主类去~
    是想用作计算出来速度和下载总进程和剩余时间什么的~
    但是发现这样传过去的一直是0~
    本人完全菜鸟~见谅~
      

  3.   

    public Download(int size){
    super("下载器");
    thisjf=this;
    this.size = size;
    zongsize += size;
    }
    在主类里面弄了个这个~
    zongsize += size;   这句话换几个地方放过~好像都没用~
      

  4.   

    byte[] buffer = new byte[10240]; //设置缓冲区大小
    int bytesRead;
    size = 0; //该线程下载总大小
    InputStream ins = aa.getInputStream();
    while ((bytesRead = ins.read(buffer)) != -1) {
    size += bytesRead;
    if (size > length) {
    bytesRead = (int) (bytesRead - (size - length));
    size = (int) length;
    }
    Download pro = new Download(size);
    randout.write(buffer, 0, bytesRead);
    }
    randout.close();
    就是这个里面把每次下载的总大小SIZE传到另一个类里面的~
    Download pro = new Download(size);在那个类里面累加~
    一直不能实现~
      

  5.   

    个人意见:我感觉应该是 size = 0 这里出现错误。。你每次都把都把size 赋值为0 我觉得应该是 把 size 设置为静态的。。希望我的回答能帮助到楼主、如果我的回答有错请修改。。 
      

  6.   

    这个赋值是在while语句之前哈~
    在这个线程下载完成之前不会跳出去的~我也是把每次下载一小部分之后把SIZE传过去的~
      

  7.   

    同意上楼:我感觉应该是 size = 0 这里出现错误。。你每次都把都把size 赋值为0 我觉得应该是 把 size 设置为静态的。。希望我的回答能帮助到楼主、如果我的回答有错请修改。。