单线程下载器

来源:互联网 发布:购物网站seo 编辑:程序博客网 时间:2024/04/29 05:22

这个是界面,输入的必须是直接的下载地址。

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
packagenet;
 
importjava.awt.FlowLayout;
importjava.awt.Font;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.net.URL;
importjava.net.URLConnection;
 
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
 
publicclass SingleThreadDownload extendsJFrame implementsActionListener{
    publicSingleThreadDownload(){
        panel.setLayout(newFlowLayout());
        label1.setFont(newFont("雅黑", Font.BOLD, 15));
        panel.add(label1);
        panel.add(label2);
        panel.add(urlField);
        panel.add(StartButton);
        panel.add(resetButton);
        panel.add(exitButton);
        setContentPane(panel);
        StartButton.addActionListener(this);
        resetButton.addActionListener(this);
        exitButton.addActionListener(this);
        setSize(400,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    publicvoid download(String address) throwsException{
        URL url = newURL(address);
        URLConnection urlcon = url.openConnection();
        urlcon.connect();
        InputStream in = urlcon.getInputStream();
        String filePath = url.getFile();
        intpos = filePath.lastIndexOf("/");
        String fileName = filePath.substring(pos + 1);
        FileOutputStream out = newFileOutputStream("D:\\"+ fileName);
        byte[] bytes = newbyte[1024];
        intlen = in.read();
        while(len != -1){
            out.write(bytes,0, len);
            len = in.read();
        }
        out.close();
        in.close();
        JOptionPane.showMessageDialog(this,"下载完毕");
    }
 
    publicstatic void main(String[] args){
        newSingleThreadDownload();
    }
 
    privatefinal JPanel panel = newJPanel();
    privatefinal JLabel label1 = newJLabel("网络资源的单线程下载:");
    privatefinal JLabel label2 = newJLabel("网络资源的网址:");
    JButton StartButton = newJButton("点击开始下载");
    JButton resetButton = newJButton("清空");
    JButton exitButton = newJButton("退出");
    JTextField urlField = newJTextField(20);
 
    @Override
    publicvoid actionPerformed(ActionEvent e){
        if(e.getSource() == StartButton){
            if("".equals(urlField.getText())){
                JOptionPane.showMessageDialog(this,"请输入资源地址");
            }
            String url = urlField.getText();
            try{
                download(url);
            }catch(Exception e1){
                JOptionPane.showMessageDialog(this,"资源地址有误,请检查,谢谢!");
                e1.printStackTrace();
            }
        }elseif(e.getSource() == resetButton){
            urlField.setText("");
        }else{
            System.exit(0);
        }
 
    }
 
}
原创粉丝点击