`

JAVA中HTTP的多线程下载程序

阅读更多
  JAVA的HTTP多线程下载程序。自己教学中使用的一段程序。
java 代码
 
  1. public class DownloadNetTest {  
  2.    private File fileOut;  
  3.    private URL url;  
  4.    private long fileLength=0;  
  5.    //初始化线程数  
  6.    private int ThreadNum=5;  
  7.    public DownloadNetTest(){  
  8.    try{  
  9.       System.out.println("正在链接URL");  
  10.       url=new URL("http://211.64.201.201/uploadfile/nyz.mp3");  
  11.       HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();  
  12.       fileLength=urlcon.getContentLength();  
  13.       if(urlcon.getResponseCode()>=400){  
  14.       System.out.println("服务器响应错误");  
  15.       System.exit(-1);  
  16.       }  
  17.       if(fileLength<=0)  
  18.       System.out.println("无法获知文件大小");  
  19.       //打印信息  
  20.       printMIME(urlcon);  
  21.       System.out.println("文件大小为"+fileLength/1024+"K");  
  22.       //获取文件名  
  23.       String trueurl=urlcon.getURL().toString();  
  24.       String filename=trueurl.substring(trueurl.lastIndexOf('/')+1);  
  25.       fileOut=new File("D://",filename);  
  26.    }  
  27.    catch(MalformedURLException e){  
  28.       System.err.println(e);  
  29.    }  
  30.    catch(IOException e){  
  31.       System.err.println(e);  
  32.    }  
  33.    init();  
  34. }  
  35.    private void init(){  
  36.       DownloadNetThread [] down=new DownloadNetThread[ThreadNum];  
  37.    try {  
  38.       for(int i=0;i<ThreadNum;i++){  
  39.          RandomAccessFile randOut=new RandomAccessFile(fileOut,"rw");  
  40.          randOut.setLength(fileLength);  
  41.          long block=fileLength/ThreadNum+1;  
  42.          randOut.seek(block*i);  
  43.          down[i]=new DownloadNetThread(url,randOut,block,i+1);  
  44.          down[i].setPriority(7);  
  45.          down[i].start();  
  46.       }  
  47.    //循环判断是否下载完毕  
  48.    boolean flag=true;  
  49.    while (flag) {  
  50.       Thread.sleep(500);  
  51.       flag = false;  
  52.       for (int i = 0; i < ThreadNum; i++)  
  53.       if (!down[i].isFinished()) {  
  54.       flag = true;  
  55.       break;  
  56.       }  
  57.   }// end while  
  58.    System.out.println("文件下载完毕,保存在"+fileOut.getPath() );  
  59.    } catch (FileNotFoundException e) {  
  60.          System.err.println(e);  
  61.          e.printStackTrace();  
  62.    }  
  63.    catch(IOException e){  
  64.       System.err.println(e);  
  65.       e.printStackTrace();  
  66.    }  
  67.    catch (InterruptedException e) {  
  68.    System.err.println(e);  
  69.    }  
  70. }  
  71. private void printMIME(HttpURLConnection http){  
  72.    for(int i=0;;i++){  
  73.    String mine=http.getHeaderField(i);  
  74.    if(mine==null)  
  75.    return;  
  76.    System.out.println(http.getHeaderFieldKey(i)+":"+mine);  
  77.    }  
  78. }  
  79. public static void main(String[] args) {  
  80.        DownloadNetTest app=new DownloadNetTest();  
  81. }  
  82. }  
分享到:
评论
4 楼 xiaofan_0204 2011-12-09  
有点恶心呀,少了一个类……………………
3 楼 h286271819 2011-03-07  
你奶奶,DownloadNetThread 类呢?
2 楼 cooc123 2008-09-15  
DownloadNetThread 类呢?
1 楼 sparon 2008-03-21  
请教楼主:
    因为我是使用动态加载驱动JAR包的,做了个数据库接口,但是现在需要用户提供DRIVERS,我想做到程序自动识别JAR包中继承了DRIVER接口的类作为DIRVERS,请问楼主知不知道如何做啊?

相关推荐

Global site tag (gtag.js) - Google Analytics