关闭

多线程下载器

315人阅读 评论(0) 收藏 举报

多线程下载器

?
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package net;
 
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class DownMultiThreadFrame extends JFrame implements ActionListener{
    public DownMultiThreadFrame(){
        panel.setLayout(new FlowLayout());
        label1.setFont(new Font("雅黑", 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);
    }
 
    public static void main(String[] args){
        new DownMultiThreadFrame();
 
    }
 
    private final JPanel panel = new JPanel();
    private final JLabel label1 = new JLabel("网络资源的多线程下载:");
    private final JLabel label2 = new JLabel("网络资源的网址:");
    JButton StartButton = new JButton("点击开始下载");
    JButton resetButton = new JButton("清空");
    JButton exitButton = new JButton("退出");
    JTextField urlField = new JTextField(20);
 
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == StartButton){
            if("".equals(urlField.getText())){
                JOptionPane.showMessageDialog(this, "请输入资源地址");
            }
            String url = urlField.getText();
            int pos = url.lastIndexOf("/");
            String fileName = url.substring(pos + 1);
            try{
                download(url, "d:\\" + fileName, 2);
            }catch(Exception e1){
                e1.printStackTrace();
            }
        }else if(e.getSource() == resetButton){
            urlField.setText("");
        }else{
            System.exit(0);
        }
    }
 
    /**
     * 下载网络资源
     * */
    public void download(String url, String dest, int threadNum)
            throws Exception{
        URL downURL = new URL(url);
        // 打开网络连接
        HttpURLConnection conn = (HttpURLConnection) downURL.openConnection();
        // 创建文件长度的变量
        long fileLength = -1;
        // 获得连接状态标记代码
        int stateFlagCode = conn.getResponseCode();
        if(stateFlagCode == 200){
            // 网络连接正常
            fileLength = conn.getContentLength();
            // 取笑网络连接
            conn.disconnect();
        }
        if(fileLength > 0){
            // 计算每个线程的字节数
            long byteCounts = fileLength / threadNum + 1;
            File file = new File(dest);
            int i = 0;
            while(i < threadNum){
                long startPosition = byteCounts * i;
                long endPosition = byteCounts * (i + 1);
                if(i == threadNum - 1){
                    DownMulltiThread fileThread = new DownMulltiThread(url,
                            file, startPosition, 0);
                    new Thread(fileThread).start();
                }else{
                    DownMulltiThread fileThread = new DownMulltiThread(url,
                            file, startPosition, endPosition);
                    new Thread(fileThread).start();
                }
                i++;
            }
        }
    }
 
}// end class
 
/**
 * 用于实现网络资源的下载
 * */
class DownMulltiThread implements Runnable{
 
    public DownMulltiThread(){
 
    }
 
    /**
     * @param sUrl
     *            网络资源位置
     * @param desFile
     *            需要写入的文件对象
     * @param startPos
     *            写入的开始位置
     * @param endPos
     *            写入文件的结束位置
     * */
    public DownMulltiThread(String sUrl, File desFile, long startPos,
            long endPos){
        this.desFile = desFile;
        this.sUrl = sUrl;
        this.startPos = startPos;
        this.endPos = endPos;
    }
 
    private String sUrl = "";
    private File desFile;
    private long startPos;
    private long endPos;
 
    @Override
    public void run(){
        RandomAccessFile out;
        try{
            out = new RandomAccessFile(desFile, "rw");
            out.seek(startPos);
            URL url = new URL(sUrl);
            URLConnection urlcon = url.openConnection();
            urlcon.connect();
            InputStream in = urlcon.getInputStream();
            BufferedInputStream buf = new BufferedInputStream(in);
            byte[] bytes = new byte[1024];
            int len = buf.read(bytes);
            while(len != -1){
                out.write(bytes, 0, len);
                len = buf.read(bytes);
            }
 
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:30174次
    • 积分:409
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:42篇
    • 译文:0篇
    • 评论:0条
    文章分类