多线程下载器

转载 2012年03月25日 18:22:38

多线程下载器

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

相关文章推荐

多线程下载器 断点续传

  • 2015-06-30 11:34
  • 89KB
  • 下载

多线程断点下载器

  • 2013-09-25 14:31
  • 93KB
  • 下载

Android开发多线程断点续传下载器

使用多线程断点续传下载器在下载的时候多个线程并发可以占用服务器端更多资源,从而加快下载速度,在下载过程中记录每个线程已拷贝数据的数量,如果下载中断,比如无信号断线、电量不足等情况下,这就需要使用到断点...

IDM多线程下载器

  • 2015-05-25 22:19
  • 4.64MB
  • 下载

Android开发多线程断点续传下载器

使用多线程断点续传下载器在下载的时候多个线程并发可以占用服务器端更多资源,从而加快下载速度,在下载过程中记录每个线程已拷贝数据的数量,如果下载中断,比如无信号断线、电量不足等情况下,这就需要使用到断点...

java Swing 多线程下载器

  • 2009-05-16 11:04
  • 100KB
  • 下载

JAVA多线程下载器

大二末了,选好了方向,而去也喜欢网络并发,多线程之类的,就决定做个多线程下载器,学习学习文件,网络,线程间通讯的方法。 代码其实早就开始写,只是一直比较忙,也没写多少。今天认真花了一天把下载线程写了。...

android多线程下载器实例

  • 2013-04-12 09:23
  • 92KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)