⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame1.java~8~

📁 简易多线程下载程序
💻 JAVA~8~
字号:
package mythead;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.io.*;
import java.net.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Frame1 extends JFrame {
  JPanel contentPane;
  String currentFileName;
  String ipAddress;
  String ipPort;
  String downLoadURL;
  int fileLength;
  int threadCount;
 RandomAccessFile dst = null;

  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanel1 = new JPanel();
  JPanel jPanel2 = new JPanel();
  XYLayout xYLayout2 = new XYLayout();
  JRadioButton jRadioButton1 = new JRadioButton();
  JLabel jLabel1 = new JLabel();
  JTextField jTextField1 = new JTextField();
  JLabel jLabel2 = new JLabel();
  JTextField jTextFieldIpProt = new JTextField();
  JPanel jPanel3 = new JPanel();
  XYLayout xYLayout3 = new XYLayout();
  JTextField jTextFieldDownAddr = new JTextField();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JTextField jTextFieldThread = new JTextField();
  JButton jButton1 = new JButton();
  JProgressBar jProgressBar1 = new JProgressBar();
  JLabel jLabel5 = new JLabel();
  JTextField jTextFieldSaveAddr = new JTextField();
  JButton jButton2 = new JButton();
  JFileChooser jFileChooser1 = new JFileChooser();  BorderLayout borderLayout2 = new BorderLayout();

  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Frame Title");
    jPanel1.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    jPanel1.setLayout(borderLayout2);
    jPanel2.setBorder(BorderFactory.createEtchedBorder());
    jPanel2.setLayout(xYLayout2);
    jRadioButton1.setText("是否使用代理");
    jLabel1.setText("地址:");
    jTextField1.setText("");
    jLabel2.setText("端口:");
    jTextFieldIpProt.setText("");
    jPanel3.setLayout(xYLayout3);
    jLabel3.setText("下载地址:");
    jTextFieldDownAddr.setText("");
    jLabel4.setText("线程数:");
    jPanel3.setBorder(BorderFactory.createEtchedBorder());
    jButton1.setText("确定");
    jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
    jProgressBar1.setMaximum(100);
    jTextFieldThread.setText("");
    jLabel5.setText("保存地址:");
    jTextFieldSaveAddr.setText("");
    jButton2.setText("浏览.....");
    jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
    contentPane.add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jPanel2, BorderLayout.NORTH);
    jPanel2.add(jRadioButton1,   new XYConstraints(14, 6, 107, 23));
    jPanel2.add(jLabel1,   new XYConstraints(20, 46, 48, 21));
    jPanel2.add(jTextField1,  new XYConstraints(74, 45, 143, 24));
    jPanel2.add(jLabel2,  new XYConstraints(229, 45, 55, 23));
    jPanel2.add(jTextFieldIpProt,                                      new XYConstraints(265, 43, 70, 25));
    jPanel1.add(jPanel3, BorderLayout.CENTER);
    jPanel3.add(jTextFieldDownAddr,                                                                  new XYConstraints(72, 11, 306, 25));
    jPanel3.add(jLabel3,  new XYConstraints(9, 9, 60, 28));
    jPanel3.add(jButton1,    new XYConstraints(139, 168, 90, 30));
    jPanel3.add(jLabel4,  new XYConstraints(11, 77, 56, 25));
    jPanel3.add(jTextFieldThread, new XYConstraints(73, 78, 77, 26));
    jPanel3.add(jTextFieldSaveAddr,      new XYConstraints(72, 44, 223, 25));
    jPanel3.add(jLabel5,     new XYConstraints(9, 45, 60, 28));
    jPanel3.add(jButton2,  new XYConstraints(309, 45, 68, 24));
    jPanel3.add(jProgressBar1, new XYConstraints(5, 114, 380, 28));
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void jButton2_actionPerformed(ActionEvent e) {
    jFileChooser1.showSaveDialog(this);
    currentFileName = jFileChooser1.getSelectedFile().getPath();
    this.jTextFieldSaveAddr.setText(currentFileName) ;

  }

  void jButton1_actionPerformed(ActionEvent e) {

    try {
      this.ipAddress = this.jTextField1.getText();
      this.ipPort = this.jTextFieldIpProt.getText();
      this.currentFileName = this.jTextFieldSaveAddr.getText();
      this.downLoadURL = this.jTextFieldDownAddr.getText();
      this.threadCount = Integer.parseInt(this.jTextFieldThread.getText());
      this.dst = new RandomAccessFile(currentFileName, "rw");
      System.getProperties().put("proxySet", "true");
      System.getProperties().put("proxyHost", this.ipAddress);
      System.getProperties().put("proxyPort", this.ipPort);
//http://www.nyist.net/nimages/barner1.gif
      URL url = new URL(this.downLoadURL);
      HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
      this.fileLength = httpConnection.getHeaderFieldInt("Content-Length", -1);
      httpConnection.disconnect();
      int k = this.fileLength / this.threadCount;
      myClass myThread[] = new myClass[this.threadCount];
      for (int i = 0; i < this.threadCount; i++) {
        myThread[i] = new myClass(this.downLoadURL, this.dst, i * k, i * k + k);
        myThread[i].start();
      }
    }
    catch (IOException ex) {
    }
    catch (NumberFormatException ex) {
      System.out.println("aaaaaaaaaaaaaaaaaaaa"+ex.toString());
    }

  }
}

class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton2_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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