本帖最后由 yuze311 于 2012-05-28 21:41:25 编辑

解决方案 »

  1.   

    //新建下载对话框,button2按钮是 确定 按钮
    package index;import index.download.*;import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.File;
    public class NewTaskFrame extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JLabel label1;
    private JTextField textField1;

    private JLabel label2;
    private JTextField textField2;

    private JLabel label3;
    private JTextField textField3;
    private JButton button1;

    //private JLabel label4;
    //private JComboBox combox;

    private JButton button2;
    private JButton button3;

    private String urlAdress;
    private String fileName;
    private String savePath;
    //private int threadCount;
    private JFileChooser fileChooser;
    public NewTaskFrame(){
    setLayout(null);
    label1=new JLabel("下载地址:");
    label1.setBounds(30,50,55,20);
    label2=new JLabel("保存目录");
    label2.setBounds(30,85,55,20);
    label3=new JLabel("文件名:");
    label3.setBounds(30,120,55,20);
    /*label4=new JLabel("线程数:");
    label4.setBounds(30,155,50,20);*/
    textField1=new JTextField(30);
    textField1.setBounds(100,50,400,20);
    textField2=new JTextField(System.getProperty("user.home"),22);
    textField2.setBounds(100,85,400,20);
    textField3=new JTextField(5);
    textField3.setBounds(100,120,290,20);
    button1=new JButton("选择目录");
    button1.setBounds(420,120,80,20);
    /*combox=new JComboBox();
    for(int i=1;i<=4;i++){
    combox.addItem(i);
    }
    combox.setBounds(100,155,50,20);*/
        button2=new JButton("确定");
        button2.setBounds(200,190,65,30);
        button3=new JButton("取消");
        button3.setBounds(330,190,65,30);
        add(label1);
        add(textField1);
        add(textField2);
        add(textField3);
        add(button1);
        //add(combox);
        add(label1);
        add(label2);
        add(label3);
        //add(label4);
        add(button2);
        add(button3);
          //添加按钮事件
        button2.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
         if(textField1.getText().equals(""))
         JOptionPane.showMessageDialog(null,"下载地址不能为空!");
         else{
         urlAdress=textField1.getText();
         //System.out.println(urlAdress);
         }
        
         if(textField3.getText().equals("")){
         JOptionPane.showMessageDialog(null,"文件名不能为空!");
         }
         else{
         fileName=textField3.getText();
         //System.out.println(fileName);
         }
         savePath=textField2.getText();
        
         Source source=new Source(urlAdress,savePath,fileName);
         Context.downloadContent.sources.add(source);
         Context.startDownload.startLoad(source);
         setVisible(false);
         }
        });
        
        button3.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
         setVisible(false);
         }
        });
        button1.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
         if(fileChooser==null)
         fileChooser=new JFileChooser(new File(System.getProperty("user.home")));
         fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);      fileChooser.showOpenDialog(null);
         fileChooser.setCurrentDirectory(fileChooser.getSelectedFile());
         textField2.setText(fileChooser.getSelectedFile().getAbsolutePath());
         }
        });
        try{
            javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");          
            }catch(Exception e){
                   e.printStackTrace();
            } setTitle("新建下载任务");
    setResizable(false);
    Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(screen.width/4,screen.height/4,590,300);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    }
      

  2.   

    //对下载资源处理package index.download;public class StartDownload {
    public void startLoad(Source source){
    try{
    int fileLength=source.getFileLength();
    int numberPerThread=fileLength/4;
    for(int i=0;i<4;i++){
    int length=numberPerThread;
    if(i==3){
    length=fileLength-i*numberPerThread;
    }
    Part part=new Part(i*numberPerThread,length);
    source.getParts().add(part);
    DownloadThread downloadThread=new DownloadThread(source,part);
    downloadThread.start();
    }


    }
    catch(Exception e){
    e.printStackTrace();
    }
    }
    }
      

  3.   

    //下载线程
    package index.download;
    import java.io.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    class DownloadThread extends Thread{
    private URL url=null;
    private InputStream is;
    private RandomAccessFile raf;
    private Source source;
    private Part part;
    public DownloadThread(Source source,Part part){
    this.source=source;
    this.part=part;
    try {
    raf=new RandomAccessFile(source.getSavePath()+File.separator+part.getPartName(),"rw");
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    public void run(){
    try {
    url=new URL(source.getUrlAddress());
    } catch (MalformedURLException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }
    byte [] buf=new byte[32];
    int perRead=0;
    try {
    URLConnection connection=url.openConnection();
    connection.connect();
    is=connection.getInputStream();
    is.skip(part.getStart()+part.getCurrentLoad());
    raf.seek(part.getCurrentLoad());
    int per=0;
    while((perRead=is.read(buf))!=-1){
    per += perRead;
    if(per>=32){
    raf.write(buf,0,32-(part.getLength()-per));
    break;
    }
    raf.write(buf,0,perRead);
    part.setCurrentLoad(part.getCurrentLoad()+perRead);
    }
    if(isFinish(source))
    merger(source);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    try{
    if(is!=null)
    is.close();
    if(raf!=null)
    raf.close();
    }
    catch(IOException e){
    e.printStackTrace();
    }
    }
    public boolean isFinish(Source source){
    int totalLoad=0;
    ArrayList<Part> parts=source.getParts();
    for(Part part : parts){
    totalLoad += part.getCurrentLoad();
    }
    if(totalLoad==source.getFileLength()){
    return true;
    }
    else
    return false;
    }
    public void merger(Source source) throws IOException{
    ArrayList<Part> parts=source.getParts();
    RandomAccessFile ran=new RandomAccessFile(source.getSavePath()+File.separator+source.getFileName(),"rw");
    for(Part part : parts){
    File file=new File(source.getSavePath()+File.separator+part.getPartName());
    InputStream is=new FileInputStream(file);
    byte [] buffer=new byte[32];
    int hasRead=0;
    while((hasRead=is.read())!=-1){
    ran.write(buffer,0,hasRead);
    }
    is.close();
    }
    ran.close();
    }
    }
      

  4.   


    at index.download.StartDownload.startLoad(StartDownload.java:24)
    at index.NewTaskFrame$1.actionPerformed(NewTaskFrame.java:93)都把行号给些出来了对应你哪两行阿?
      

  5.   

    LZ Debug一下,下StartDownload.java:24行,空指针。
    感觉source 或者source.getParts() 是 null,怀疑 url,path,等没设置对。
      

  6.   

    package index.download;import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;public class Source {
    private String urlAddress;
    private String savePath;
    private String fileName;
    private ArrayList<Part> parts;
    这个ArrayList<Part> parts 没有初始化
    鉴定完毕
      

  7.   

    public class Source
    private ArrayList<Part> parts;没有初始化并且没有set方法。getParts()方法永远返回null.
      

  8.   

    自己debug调试一下咯!看看那个值为null
      

  9.   

    又看了下,真的是ArrayList<Part> parts 没初始化,过了
      

  10.   

    通常这种情况,如果看不出来,那怎么用debug或者其他方法测出空指针呢?
      

  11.   

    空指针是最常见的,要养成debug的习惯