TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
多线程下载的例子,如果问题,希望讨论下.
- import java.io.*;
- import java.net.*;
- public class MyMutilDown {
- /**
- * 单线程的远程下载
- */
- public void testOneTDown(String filePath, String url) {
- try {
- // 要写入的文件
- File file = new File(filePath + getFileExtName(url));
- FileWriter fWriter = new FileWriter(file);
- URL ul = new URL(url);
- URLConnection conn = ul.openConnection();
- conn.setConnectTimeout(2000);// 请求超时时间
- // int len = conn.getContentLength();
- InputStream in = conn.getInputStream();
- // byte[] by = new byte[1024];
- int temp = 0;
- while ((temp = in.read()) != -1) {
- fWriter.write(temp);
- }
- fWriter.close();
- in.close();
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- /**
- * 文件后缀名
- *
- * @param path
- * @return
- */
- public String getFileExtName(String path) {
- return path.substring(path.lastIndexOf("."));
- }
- /**
- * 测试多线程
- *
- * @param filePath
- * 文件保存路径
- * @param url
- * url
- * @param tnum
- * 线程数量
- */
- public void testMoreTDown(String filePath, String url, int tnum) {
- try {
- // 要写入的文件
- final File file = new File(filePath + getFileExtName(url));
- RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");// 建立随机访问
- final URL ul = new URL(url);
- HttpURLConnection conn = (HttpURLConnection) ul.openConnection();
- conn.setConnectTimeout(2000);// 请求超时时间
- conn.setRequestMethod("GET");
- int len = conn.getContentLength();// 文件长度
- accessFile.setLength(len);
- accessFile.close();
- final int block = (len + tnum - 1) / tnum;// 每个线程下载的快大小
-
- for (int i = 0; i < tnum; i++) {
- final int a = i;
- new Thread(new Runnable() {
- int start = block * a;// 开始位置
- int end = block * (a + 1) - 1;// 结束位置
- @Override
- public void run() {
- HttpURLConnection conn2 = null;
- RandomAccessFile accessFile2 = null;
- InputStream in = null;
- try {
- conn2 = (HttpURLConnection) ul.openConnection();
- conn2.setConnectTimeout(2000);// 请求超时时间
- conn2.setRequestMethod("GET");
- // TODO Auto-generated method stub
- conn2.setRequestProperty("Range", "bytes=" + start
- + "-" + end + "");// 设置一般请求属性 范围
- in = conn2.getInputStream();
- byte[] data = new byte[1024];
- int len = 0;
- accessFile2 = new RandomAccessFile(file, "rwd");
- accessFile2.seek(start);
-
- while ((len = in.read(data)) != -1) {
- accessFile2.write(data, 0, len);
- }
- System.out.println("线程:" + a + "下载完成!");
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } finally {
- try {
- accessFile2.close();
- in.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- }
- }).start();
-
- }
-
-
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- MyMutilDown mydown = new MyMutilDown();
- String path = "http://www.java3z.com/cwbwebhome/dir1/dir5/mon664.zip";
- // mydown.downLoad(path, "D:\aa", 1);
- // mydown.testOneTDown("D:", path);
- mydown.testMoreTDown("c:\ex\ok", path, 3);
- }
- }
复制代码 运行:
C:ex>java MyMutilDown
线程:0下载完成!
线程:2下载完成!
线程:1下载完成!
源码下载:http://file.javaxxz.com/2014/10/30/235829281.zip |
|