hyljava

          java多線程下載

          import java.io.BufferedInputStream;
          import java.io.IOException;
          import java.io.RandomAccessFile;
          import java.net.HttpURLConnection;
          import java.net.URL;
          import java.net.URLConnection;
          import java.util.concurrent.CountDownLatch;
          import java.util.concurrent.ExecutorService;
          import java.util.concurrent.Executors;


          public class FileDownLoadTest {
           
           
           private static final int TCOUNT = 10;
           
           private CountDownLatch latch = new CountDownLatch(TCOUNT);

           private long completeLength = 0;
           
           private long fileLength;
           /**
            * @param args
            * @throws Exception
            */
           public static void main(String[] args) throws Exception {
            
            new FileDownLoadTest().download("http://localhost:8080/test/IESSAction.docx");
           }
           
           
           public void download(String address) throws Exception{
            ExecutorService service = Executors.newFixedThreadPool(TCOUNT);
            URL url = new URL(address);
            URLConnection cn = url.openConnection();
            cn.setRequestProperty("Referer", "http://www.test.com");
            fileLength = cn.getContentLength();
            long packageLength = fileLength/TCOUNT;
            long leftLength = fileLength%TCOUNT;
            RandomAccessFile file = new RandomAccessFile("d:\\test.docx","rw");
            //計算每個線程請求文件的開始和結束位置
            long pos = 0;
            long endPos = pos + packageLength;
            for(int i=0; i<TCOUNT; i++){
             if(leftLength >0){
              endPos ++;
              leftLength--;
             }
             service.execute(new DownLoadThread(url, file, pos, endPos));
             pos = endPos;
             endPos = pos + packageLength;
            }
            System.out.println("waiting........................................");
            long begin = System.currentTimeMillis();
            latch.await();
            file.close();
            System.out.println("end........................................");
            System.out.println(System.currentTimeMillis() - begin + "ms");
            service.shutdown();
           }
           
           class DownLoadThread implements Runnable{
            
            private URL url;
            private RandomAccessFile file;
            private long from;
            private long end;
            
            DownLoadThread(URL url, RandomAccessFile file, long from, long end){
             this.url = url;
             this.file = file;
             this.from = from;
             this.end = end;
            }
            
            
            public void run() {
             long pos = from;
             byte[] buf = new byte[512];
             try {
              HttpURLConnection cn = (HttpURLConnection) url.openConnection();
              cn.setRequestProperty("Range", "bytes=" + from + "-" + end);
              if(cn.getResponseCode() != 200 && cn.getResponseCode()!=206){
               run();
               return;
              }
              BufferedInputStream bis = new BufferedInputStream(cn.getInputStream());
              int len ;
              while((len = bis.read(buf)) != -1){
          //     synchronized(file){
                file.seek(pos);
                file.write(buf, 0, len);
          //     }
               pos += len;
               completeLength +=len;
               System.out.println("threadName: " + Thread.currentThread().getName()
                 + "persent: " + completeLength * 100 /fileLength + "%");
              }
              cn.disconnect();
              latch.countDown();
             } catch (IOException e) {
              e.printStackTrace();
              
             }
            }
           }
          }

          posted on 2012-11-30 11:57 何云隆 閱讀(262) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 杨浦区| 广灵县| 长春市| 桃江县| 商丘市| 嘉善县| 榕江县| 邳州市| 新宁县| 五家渠市| 芜湖县| 永新县| 麻栗坡县| 天台县| 临海市| 光山县| 钟祥市| 铜鼓县| 永丰县| 巴林左旗| 和顺县| 永修县| 鸡东县| 嵩明县| 鹿邑县| 拜泉县| 博乐市| 驻马店市| 朝阳市| 吉木萨尔县| 岗巴县| 象山县| 新闻| 武陟县| 琼结县| 根河市| 光泽县| 榕江县| 云南省| 沧源| 遵化市|