多線程下載器

多線程下載器

?
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();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章