如题
还有就是如何用多线程序下载文件
最好有代码解释

解决方案 »

  1.   

    public void saveToFile(String destUrl, String fileName) throws IOException 
    {
         FileOutputStream fos = null;
         BufferedInputStream bis = null;
         HttpURLConnection httpUrl = null;
         URL url = null;
         byte[] buf = new byte[BUFFER_SIZE];
         int size = 0;
         int s=0;
         //建立链接
        url = new URL(destUrl);
         httpUrl = (HttpURLConnection) url.openConnection();
         //连接指定的资源
         httpUrl.connect();
         //获取网络输入流
         bis = new BufferedInputStream(httpUrl.getInputStream());
         //建立文件
         fos = new FileOutputStream(fileName);
        
    System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件[" + fileName + "]");
         //保存文件
         while ( (size = bis.read(buf)) != -1) 
         {
         fos.write(buf, 0, size);
         //++s;
         System.out.println(size);
         //System.out.println(s/1024+"M");
         }
          
         fos.close();
         bis.close();
         httpUrl.disconnect();
       }
      

  2.   

    String length = httpUrl.getHeaderField("Content-Length");  //获取文件长度
      

  3.   

    请问一下Content-Length这是什么字段 获得文件信息的其它方法是什么?
      

  4.   

    我写的多线程文件下载程序
    public class DownloadNetTest { 
       private File fileOut; 
       private URL url; 
       private long fileLength=0; 
       //初始化线程数 
       private int ThreadNum=5;    public DownloadNetTest(){ 
       try{ 
          System.out.println("正在链接URL"); 
          url=new URL("http://211.64.201.201/uploadfile/nyz.mp3"); 
          HttpURLConnection urlcon=(HttpURLConnection)url.openConnection(); 
          //根据响应获取文件大小
          fileLength=urlcon.getContentLength(); 
          if(urlcon.getResponseCode()>=400){ 
          System.out.println("服务器响应错误"); 
          System.exit(-1); 
          } 
          if(fileLength<=0) 
          System.out.println("无法获知文件大小"); 
          //打印信息 
          printMIME(urlcon); 
          System.out.println("文件大小为"+fileLength/1024+"K"); 
          //获取文件名 
          String trueurl=urlcon.getURL().toString(); 
          String filename=trueurl.substring(trueurl.lastIndexOf('/')+1); 
          fileOut=new File("D://",filename); 
       } 
       catch(MalformedURLException e){ 
          System.err.println(e); 
       } 
       catch(IOException e){ 
          System.err.println(e); 
       } 
       init(); 

       private void init(){ 
          DownloadNetThread [] down=new DownloadNetThread[ThreadNum]; 
       try { 
          for(int i=0;i<ThreadNum;i++){ 
             RandomAccessFile randOut=new RandomAccessFile(fileOut,"rw"); 
             randOut.setLength(fileLength); 
             long block=fileLength/ThreadNum+1; 
             randOut.seek(block*i); 
             down[i]=new DownloadNetThread(url,randOut,block,i+1); 
             down[i].setPriority(7); 
             down[i].start(); 
          } 
       //循环判断是否下载完毕 
       boolean flag=true; 
       while (flag) { 
          Thread.sleep(500); 
          flag = false; 
          for (int i = 0; i < ThreadNum; i++) 
          if (!down[i].isFinished()) { 
          flag = true; 
          break; 
          } 
      }// end while 
       System.out.println("文件下载完毕,保存在"+fileOut.getPath() );
       } catch (FileNotFoundException e) { 
             System.err.println(e); 
             e.printStackTrace(); 
       } 
       catch(IOException e){ 
          System.err.println(e); 
          e.printStackTrace(); 
       } 
       catch (InterruptedException e) { 
       System.err.println(e); 
       } } 
    private void printMIME(HttpURLConnection http){ 
       for(int i=0;;i++){ 
       String mine=http.getHeaderField(i); 
       if(mine==null) 
       return; 
       System.out.println(http.getHeaderFieldKey(i)+":"+mine); 
       } 
    } public static void main(String[] args) { 
           DownloadNetTest app=new DownloadNetTest(); 
    } } 
    //线程类
    public class DownloadNetThread extends Thread{ 
    private InputStream randIn; 
    private RandomAccessFile randOut; 
    private URL url; 
    private long block; 
    private int threadId=-1; 
    private boolean done=false;    public DownloadNetThread(URL url,RandomAccessFile out,long block,int threadId){ 
       this.url=url; 
       this.randOut=out; 
       this.block=block; 
       this.threadId=threadId; 

     public void run(){ 
       try{ 
          HttpURLConnection http=(HttpURLConnection)url.openConnection(); 
          http.setRequestProperty("Range","bytes="+block*(threadId-1)+"-"); 
          randIn=http.getInputStream(); 
       } 
       catch(IOException e){ 
          System.err.println(e); 
       }    //////////////////////// 
       byte [] buffer=new byte[1024]; 
       int offset=0; 
       long localSize=0; 
       System.out.println("线程"+threadId+"开始下载"); 
       try { 
          while ((offset = randIn.read(buffer)) != -1&&localSize<=block) { 
          randOut.write(buffer,0,offset); 
          localSize+=offset; 
          } 
          randOut.close(); 
          randIn.close(); 
          done=true; 
          System.out.println("线程"+threadId+"完成下载"); 
          this.interrupt(); 
       } 
       catch(Exception e){ 
          System.err.println(e); 
       } 

      public boolean isFinished(){ 
         return done; 
      } 
    }
      

  5.   

    同胞们,同志们,本贴严禁灌水呀,本贴不需要你的的"顶","","dream"之类话,希望你们来这里的目的,不是一味的"顶","","dream".请谅解
      

  6.   

    冒昧问一句,你们写的程序写个PUBLIC SATAIC VOID MAIN就能运行么?还需要导入什么包?
    说实话我是头一次看见纯JAVA网络应用,每句差不多都能看懂可是确实没想到写个网络下载就这么简单~~~领教了
      

  7.   

    都是下载程序,怎么没有获取文件大小的程序?估计也不会有吧,因为JAVA是以流的形式处理文件的
    最多只能看缓冲里文件的大小
      

  8.   

    楼上的没有看懂程序,获取文件大小是通过协议实现的。
    fileLength=urlcon.getContentLength();就可以获得文件大小,这是http协议里的一个字段,只是java封装好了,不用你亲自去读这些字段。建议去看一看http协议。