Chinaunix首页 | 论坛 | 博客
  • 博客访问: 868787
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2011-01-07 09:34:53

文件下载

DownLoaderTest

Java代码

  1. package junit.test;
  2.  
  3. import java.io.File;
  4. import java.io.InputStream;
  5. import java.io.RandomAccessFile;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import org.junit.Test;
  9.  
  10. public class DownLoaderTest {
  11.  
  12.     @Test
  13.     public void download() throws Exception {
  14.         String urlpath = "%202010.10.exe";
  15.         URL url = new URL(urlpath);
  16.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  17.         conn.setRequestMethod("GET");
  18.         conn.setReadTimeout(6 * 1000);
  19.         // 线程数
  20.         int threadsize = 3;
  21.         // 获取文件大小
  22.         int filesize = conn.getContentLength();
  23.         // 每条线程下载的数量
  24.         int block = filesize / 3 + 1;
  25.         conn.disconnect();
  26.         File file = new File("Firefox.exe");
  27.         RandomAccessFile randfile = new RandomAccessFile(file, "rw");
  28.         randfile.setLength(filesize);// 设置文件的大小
  29.         randfile.close();
  30.         for (int i = 0; i < threadsize; i++) {
  31.             int startposition = i * block;// 从网路文件的什么位置开始下载
  32.             RandomAccessFile threadfile = new RandomAccessFile(file, "rw");
  33.             threadfile.seek(startposition);// 从文件的什么位置开始写入
  34.             new DownloadThread(i, url, startposition, threadfile, block)
  35.                     .start();
  36.         }
  37.         byte[] quit = new byte[1];
  38.         System.in.read(quit);
  39.         while (!('q' == quit[0])) {
  40.             Thread.sleep(3 * 1000);
  41.         }
  42.     }
  43.  
  44.     private class DownloadThread extends Thread {
  45.         private int threadid;
  46.         private URL url;
  47.         private int startposition;
  48.         private RandomAccessFile threadfile;
  49.         private int block;
  50.  
  51.         public DownloadThread(int threadid, URL url, int startposition,
  52.                 RandomAccessFile threadfile, int block) {
  53.             this.threadid = threadid;
  54.             this.url = url;
  55.             this.startposition = startposition;
  56.             this.threadfile = threadfile;
  57.             this.block = block;
  58.         }
  59.  
  60.         @Override
  61.         public void run() {
  62.             try {
  63.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  64.                 conn.setRequestProperty("Range", "bytes=" + startposition
  65.                                 + "-");
  66.                 conn.setRequestMethod("GET");
  67.                 conn.setReadTimeout(6 * 1000);
  68.                 InputStream inputStream = conn.getInputStream();
  69.                 byte[] buffer = new byte[1024];
  70.                 int len = -1;
  71.                 int readfilesize = 0;
  72.                 while (readfilesize < block
  73.                         && ((len = inputStream.read(buffer)) != -1)) {
  74.                     threadfile.write(buffer, 0, len);
  75.                     readfilesize += len;// 累计下载的文件大小
  76.                 }
  77.                 threadfile.close();
  78.                 conn.disconnect();
  79.                 System.out.println((this.threadid + 1) + "线程下载完成");
  80.             } catch (Exception e) {
  81.                 e.printStackTrace();
  82.             }
  83.             super.run();
  84.         }
  85.     }
  86. }

阅读(1118) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~