?? frame1.java
字號:
package oldpramgram;import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;public class Frame1extends JFrameimplements ActionListener {JPanel jPanel0 = new JPanel(new GridLayout(7, 1, 5, 5));JPanel jPanel1 = new JPanel();JPanel jPanel2 = new JPanel();JPanel jPanel3 = new JPanel();JPanel jPanel4 = new JPanel();JPanel jPanel5 = new JPanel();JPanel jPanel6 = new JPanel();JPanel jPanel7 = new JPanel();JLabel jLabel1 = new JLabel("DownLoand Source URL :");JLabel jLabel2 = new JLabel("Save targe file as :");JLabel jLabel3 = new JLabel("DownLoand Threads !");TitledBorder titledBorder1 = new TitledBorder("");TitledBorder titledBorder2 = new TitledBorder("");JTextField jTextField1 = new JTextField("http://seawan.nyist.net/kle/rm/volume-5/Gandhi%20-%201.rm", 25);JTextField jTextField2 = new JTextField("f:/1.gif", 20);JComboBox jComboBox1 = new JComboBox(new String[] {"1", "2", "3", "4", "5", "6", "7", "8","9", "10"});JProgressBar jProgressBar1 = new JProgressBar();JButton jButton1 = new JButton("Open");JButton jButton2 = new JButton("Start");JButton jButton3 = new JButton("Stop");JButton jButton4 = new JButton("Exit");//http://mimg.163.com/0814/logo.gifpublic Frame1() {try {jbInit();}catch (Exception e) {e.printStackTrace();}}private void jbInit() throws Exception {this.setTitle("FlashGet");this.getContentPane().add(jPanel0, null);jLabel1.setBorder(titledBorder1);jLabel1.setFont(new Font("黑體", Font.BOLD, 20));jLabel1.setForeground(Color.DARK_GRAY);jPanel0.setBounds(new Rectangle(0, 0, 400, 300));jPanel0.add(jPanel1);jPanel1.add(jLabel1);jPanel0.add(jPanel2);jPanel2.add(jTextField1);jPanel0.add(jPanel3);jPanel3.add(jLabel2);jPanel0.add(jPanel4);jLabel2.setBorder(titledBorder2);jLabel2.setFont(new Font("黑體", Font.BOLD, 20));jLabel2.setForeground(Color.DARK_GRAY);jPanel4.add(jTextField2);jPanel4.add(jButton1);jPanel0.add(jPanel5);jButton1.setBorder(titledBorder2);jLabel3.setFont(new Font("黑體", Font.BOLD, 20));jLabel3.setForeground(Color.DARK_GRAY);jPanel5.add(jLabel3);jPanel5.add(jComboBox1);jComboBox1.setSelectedIndex(4);jPanel0.add(jPanel6);jPanel6.add(jProgressBar1);jPanel0.add(jPanel7);jProgressBar1.setBorder(BorderFactory.createEtchedBorder());jProgressBar1.setStringPainted(true);jProgressBar1.setMaximum(100);jProgressBar1.setMinimum(0);jButton2.setBorder(titledBorder2);jPanel7.add(jButton2);jPanel7.add(jButton3);jButton3.setBorder(titledBorder2);jPanel7.add(jButton4);jButton4.setBorder(titledBorder2);jButton1.addActionListener(this);jButton2.addActionListener(this);jButton3.addActionListener(this);jButton4.addActionListener(this);jComboBox1.addActionListener(this);}int num_threads = 5;download[] dls;int len;Timer t;public void actionPerformed(ActionEvent e) {if (e.getSource() == jComboBox1) {String s = jComboBox1.getSelectedItem().toString();this.num_threads = Integer.parseInt(s);}if (e.getSource() == jButton1) {/*JFileChooser fc = new JFileChooser();fc.setFileSelectionMode(fc.FILES_AND_DIRECTORIES);if (fc.showOpenDialog(this) == fc.APPROVE_OPTION) {fr = fc.getSelectedFile();}jTextField2.setText(fr.toString());*/JFileChooser fc = new JFileChooser();//fc.setFileSelectionMode(fc.FILES_AND_DIRECTORIES);if (fc.showSaveDialog(this) == fc.APPROVE_OPTION) {File f = fc.getSelectedFile();if (!f.exists()) {try {f.createNewFile();}catch (IOException ex1) {ex1.getStackTrace();}}jTextField2.setText(f.getAbsolutePath());}}if (e.getSource() == jButton2) {try {URL url = new URL(jTextField1.getText());System.getProperties().put("proxySet", "true");System.getProperties().put("proxyHost", "250.250.0.0");System.getProperties().put("proxyPort", "3128");File f = new File(jTextField2.getText());HttpURLConnection c = (HttpURLConnection) url.openConnection();len = c.getContentLength();dls = new download[this.num_threads];for (int i = 0; i < this.num_threads; i++) {int start = i * (len / this.num_threads);dls[i] = new download(url,f,start,start + len / this.num_threads);dls[i].start();}t = new Timer(500, new ActionListener() {public void actionPerformed(ActionEvent e) {int total = 0;for (int i = 0; i < num_threads; i++) {total += dls[i].getTotal();}jProgressBar1.setValue(Math.round(total * 100.0f / len));System.out.println("" + len + " " + total);boolean finished = true;for (int i = 0; i < num_threads; i++) {if (dls[i].isAlive())finished = false;}if (finished) {JOptionPane.showMessageDialog(null, "Finished!!!");t.stop();}}});t.start();}catch (IOException ex) {}}if (e.getSource() == jButton3) {}if (e.getSource() == jButton4) {System.exit(0);}}public static void main(String[] args) {Frame1 frame1 = new Frame1();frame1.pack();frame1.show();}}class downloadextends Thread {URL url;File f;long start, end;int total = 0;download(URL url, File f, long start, long end) {this.url = url;this.f = f;this.start = start;this.end = end;}public void run() {try {URLConnection c = url.openConnection();c.setRequestProperty("Range", "bytes=" + start + "-");InputStream is = c.getInputStream();long p = start;RandomAccessFile rf = new RandomAccessFile(f, "rw");rf.seek(start);byte[] buf = new byte[1024];while (p < end) {int n = is.read(buf);rf.write(buf, 0, n);p += n;total += n;}rf.close();is.close();}catch (IOException ex) {}}public int getTotal() {return total;}}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -