java编写的Http协议的多线程下载器

断点下载器还在实现中。。。。。。

//////////////////////////////////界面///////////////////////////////////////////

package com.company.www.me.net;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class DownLoadTool extends JFrame {
 public static void main(String[] args) {
  final DownLoadTool tool = new DownLoadTool();
  try {
   UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
   SwingUtilities.updateComponentTreeUI(tool.getContentPane());
  } catch (Exception e) {
   e.printStackTrace();
  }
  tool.setResizable(false);
  tool.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  tool.setTitle("DownLoadTool");
  int x = Toolkit.getDefaultToolkit().getScreenSize().width/2-180;
  int y = Toolkit.getDefaultToolkit().getScreenSize().height/2-90;
  tool.setLocation(x,y);
  tool.setSize(430, 280);
  tool.setLayout(new GridBagLayout());
  JLabel label = new JLabel("URL: ");
  GridBagConstraints labelCtrl = new GridBagConstraints();
  labelCtrl.gridx = 0;
  labelCtrl.gridy = 0;
  labelCtrl.gridheight = 1;
  labelCtrl.gridwidth = 1;
  labelCtrl.anchor = GridBagConstraints.WEST;
  JScrollPane scrollPane = new JScrollPane();
  final JTextArea textArea = new JTextArea();
  scrollPane.setViewportView(textArea);
  scrollPane.setPreferredSize(new Dimension(320,33));
  GridBagConstraints scrollPaneCtrl = new GridBagConstraints();
  scrollPaneCtrl.gridx = 1;
  scrollPaneCtrl.gridy = 0;
  scrollPaneCtrl.gridheight = 1;
  scrollPaneCtrl.gridwidth = 2;
  scrollPaneCtrl.anchor = GridBagConstraints.EAST;
  scrollPaneCtrl.insets = new Insets(8,0,8,0);
  
  JLabel label2 = new JLabel("To: ");
  GridBagConstraints label2Ctrl = new GridBagConstraints();
  label2Ctrl.gridx = 0;
  label2Ctrl.gridy = 1;
  label2Ctrl.gridheight = 1;
  label2Ctrl.gridwidth = 1;
  label2Ctrl.anchor = GridBagConstraints.WEST;
  final JTextField textField = new JTextField(22);
  textField.setText("C:/Documents and Settings/wangbijian/デスクトップ/WebTest/DownLoad");
  GridBagConstraints textFieldCtrl = new GridBagConstraints();
  textFieldCtrl.gridx = 1;
  textFieldCtrl.gridy = 1;
  textFieldCtrl.gridheight = 1;
  textFieldCtrl.gridwidth = 1;
  JButton Tobtn = new JButton("Choose Path...");
  Tobtn.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent actionevent) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnVal = fileChooser.showOpenDialog(tool);
     if(returnVal == JFileChooser.APPROVE_OPTION) {
      textField.setText(fileChooser.getSelectedFile().toString());
        }
  }
   });
  GridBagConstraints TobtnCtrl = new GridBagConstraints();
  TobtnCtrl.gridx = 2;
  TobtnCtrl.gridy = 1;
  TobtnCtrl.gridheight = 1;
  TobtnCtrl.gridwidth = 1;
  TobtnCtrl.anchor = GridBagConstraints.EAST;
  
  JLabel label3 = new JLabel("Thread Count: ");
  GridBagConstraints label3Ctrl = new GridBagConstraints();
  label3Ctrl.gridx = 0;
  label3Ctrl.gridy = 2;
  label3Ctrl.gridheight = 1;
  label3Ctrl.gridwidth = 1;
  label3Ctrl.insets = new Insets(8,0,0,0);
  label3Ctrl.anchor = GridBagConstraints.WEST;
  
  final JTextField threadField = new JTextField(28);
  GridBagConstraints threadFieldCtrl = new GridBagConstraints();
  threadFieldCtrl.gridx = 1;
  threadFieldCtrl.gridy = 2;
  threadFieldCtrl.gridheight = 1;
  threadFieldCtrl.gridwidth = 2;
  threadFieldCtrl.insets = new Insets(8,0,0,0);
  threadFieldCtrl.anchor = GridBagConstraints.WEST;
  
  final JScrollPane progressPane = new JScrollPane();
  progressPane.setPreferredSize(new Dimension(380,80));
  final JPanel panel = new JPanel();
  progressPane.setViewportView(panel);
  GridBagConstraints progressPaneCtrl = new GridBagConstraints();
  progressPaneCtrl.gridx = 0;
  progressPaneCtrl.gridy = 4;
  progressPaneCtrl.gridheight = 1;
  progressPaneCtrl.gridwidth = 3;
  progressPaneCtrl.anchor = GridBagConstraints.CENTER;
  
  
  JButton btn = new JButton("DownLoad");
  GridBagConstraints btnCtrl = new GridBagConstraints();
  btnCtrl.gridx = 0;
  btnCtrl.gridy = 3;
  btnCtrl.gridheight = 1;
  btnCtrl.gridwidth = 3;
  btnCtrl.insets = new Insets(18,18,18,18);
  btnCtrl.anchor = GridBagConstraints.CENTER;
  btn.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent actionevent) {
    final JLabel progressLabel = new JLabel();
    final JProgressBar progressBar = new JProgressBar();
    progressBar.setVisible(false);
    panel.add(progressLabel);
    panel.add(progressBar);
    
    final String DownLoadPath = textArea.getText();
    final String Topath = textField.getText();
    int threadCount1 = 1;
    if (threadField.getText()!=null&&!threadField.getText().trim().equals(""))
    {
     threadCount1 = Integer.parseInt(threadField.getText());
    }
    final int threadCount = threadCount1;
    if (DownLoadPath==null||"".equals(DownLoadPath)) {
     JOptionPane.showMessageDialog(tool, "Please input URL!");
     return;
    }
    else if(Topath==null||"".equals(Topath)) {
     JOptionPane.showMessageDialog(tool, "Please select save path!");
     return;
    }
    else if(Topath!=null&&DownLoadPath!=null||"".equals(Topath.trim())&&"".equals(DownLoadPath.trim())) {
     new Thread(){
      public void run() {
       progressBar.setVisible(true);
       progressBar.setValue(0);
       new DownLoadToolClass(progressBar,DownLoadPath,Topath,threadCount).DownLoadFile(progressLabel);
      }
     }.start();
    }
   }
  });
  tool.add(label,labelCtrl);
  tool.add(scrollPane,scrollPaneCtrl);
  tool.add(label2,label2Ctrl);
  tool.add(textField,textFieldCtrl);
  tool.add(Tobtn,TobtnCtrl);
  tool.add(label3,label3Ctrl);
  tool.add(threadField,threadFieldCtrl);
  tool.add(progressPane,progressPaneCtrl);
  tool.add(btn,btnCtrl);
  tool.setVisible(true);
 }
}

//////////////////////////////关键下载类//////////////////////////////////////

package com.company.www.me.net;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class DownLoadToolClass {
 String DownLoadPath;
 String Topath;
 int threadCount;
 JProgressBar progressBar;
 public DownLoadToolClass(JProgressBar progressBar,String downLoadPath, String topath,int threadCount) {
  this.progressBar = progressBar;
  this.DownLoadPath = downLoadPath;
  this.Topath = topath;
  this.threadCount = threadCount;
 }
 public void DownLoadFile(JLabel progressLabel){
  try {
   int size;
   int blockSize;
   int EndBlockSize = -1;
   URL url = new URL(DownLoadPath);
   URLConnection conn = url.openConnection();
   size = conn.getContentLength();
   File file = new File(Topath);
   RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
   randomAccessFile.setLength(size);
   randomAccessFile.close();
   if (0==size%threadCount)
    blockSize = size/threadCount;
   else{
    blockSize = size/threadCount;
    EndBlockSize = size%threadCount;
   }
   File file2 = new File(Topath+".wbj");
   if (file2.exists()) {
    file2.delete();
    file2.createNewFile();
   }
   RandomAccessFile os = new RandomAccessFile(file2,"rw");
   os.setLength(threadCount*32);
   os.close();
   this.progressBar.setIndeterminate(false);
   this.progressBar.setMaximum(size);
   for (int i = 0; i <= threadCount; i++) {
    if (i==threadCount && EndBlockSize!=-1) {
     if (i*blockSize<size) {
      multithreadDown(this.progressBar,progressLabel,i,i*blockSize,i*blockSize + EndBlockSize);
     }
    }
    else {
     if (i*blockSize<size) {
      multithreadDown(this.progressBar,progressLabel,i,i*blockSize,i*blockSize + blockSize);
     }
    }
    
   }
   
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public void multithreadDown(final JProgressBar progressBar,final JLabel progressLabel,final int threadId,final int begin,final int end){
  new Thread(){
   public void run() {
    RandomAccessFile os = null;
    RandomAccessFile randomAccessFile = null;
    try {
     byte[] b = new byte[1024];
     int fontCount;
     String ll = begin + " " + (end - 1);
     int lenght2 = 0;
     File file = new File(Topath);
     File file2 = new File(Topath+".wbj");
     os = new RandomAccessFile(file2,"rw");
     randomAccessFile = new RandomAccessFile(file,"rw");
     randomAccessFile.seek(begin);
     URL url = new URL(DownLoadPath);
     URLConnection conn = url.openConnection();
     conn.setRequestProperty("RANGE", "bytes="+begin+"-"+(end -1));
     conn.connect();
     InputStream is = conn.getInputStream();
     while ((fontCount = is.read(b))!=-1) {
      randomAccessFile.write(b,0,fontCount);
      synchronized (progressBar) {
       float size = progressBar.getMaximum();
       int downAll = progressBar.getValue() + fontCount;
       float downD = downAll;
       float d = downD/size*100;
       String progress;
       if (String.valueOf(d).length()>4) {
        progress = String.valueOf(d).substring(0, 4) +"%";
       }
       else {
        progress = String.valueOf(d)+"%";
       }
       
       progressLabel.setText(progress);
       progressBar.setValue(downAll);
       if (downAll==size) {
        progressBar.setVisible(false);
        progressLabel.setText("Download complete");
       }
      }
      
      lenght2 +=fontCount;
      os.seek(threadId*32);
      os.write((ll + " " + lenght2 + "\r\n").getBytes());
     }
     os.close();
     randomAccessFile.close();
    } catch (Exception e) {
     e.printStackTrace();
    }
    finally{
     try {
      Thread.currentThread().stop();
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }
  }.start();
 }
}

你可能感兴趣的:(java编写的Http协议的多线程下载器)