斷點續傳

public GetRemoteFile_Client_GoOn() {  
    }  

    private boolean FileExist(String pathAndFile){//肯定文件是否已經下載,但沒有下載完成  
        File file = new File(pathAndFile);  
        if (file.exists())  
          return true;  
        else  
          return false;  
      }  
    private long FileSize(String pathAndFile){//肯定已經下載了的文件大小  
        File file = new File(pathAndFile);  
        return file.length();  
      }  
    private void FileRename(String fName,String nName){//將下載徹底的文件改名,去掉.tp名  
        File file = new File(fName);  
        file.renameTo(new File(nName));  
        file.delete();  
      }  
    public static void main(String[] args) {  
        URL url = null;  
        HttpURLConnection urlc = null;  
        DataOutputStream dos = null;  
        BufferedInputStream bis = null;  
        FileOutputStream fos = null;  
        String localFile = "d:\\XunXian_V2.0.21.1_dl.exe";//文件保存的地方及文件名,具體狀況能夠改  
        String localFile_bak = localFile + ".tp";//未下載完文件加.tp擴展名,以便於區別  
        GetRemoteFile_Client_GoOn gco = new GetRemoteFile_Client_GoOn();  
        long fileSize = 0;  
        long start = System.currentTimeMillis();  
        int len = 0;  
        byte[] bt = new byte[1024];  
        RandomAccessFile raFile=null;  
        long TotalSize = 0;//要下載的文件總大小  
        try {  
          url = new URL("http://down.qq.com/xunxian/full/XunXian_V2.0.21.1_dl.exe");        
          urlc = (HttpURLConnection) url.openConnection();  
          TotalSize = Long.parseLong(urlc.getHeaderField("Content-Length"));  
          System.out.println("下載文件大小爲:"+TotalSize);  
//        urlc.disconnect();//先斷開,下面再鏈接,不然下面會報已經鏈接的錯誤  
          urlc = (HttpURLConnection) url.openConnection();  
          //肯定文件是否存在  
          if (gco.FileExist(localFile_bak)){//採用斷點續傳,這裏的依據是看下載文件是否在本地有.tp有擴展名同名文件  
            System.out.println("文件續傳中...");  
            fileSize = gco.FileSize(localFile_bak); //取得文件在小,以便肯定隨機寫入的位置  
            System.out.println("fileSize:" + fileSize);  
            //設置User-Agent  
            //urlc.setRequestProperty("User-Agent","NetFox");  
            //設置斷點續傳的開始位置  
            urlc.setRequestProperty("RANGE", "bytes=" + fileSize + "-");  
            //urlc.setRequestProperty("RANGE", "bytes="+fileSize);//這樣寫不行,不能少了這個"-".  
            //設置接受信息  
            urlc.setRequestProperty("Accept","image/gif,image/x-xbitmap,application/msword,*/*");          
            raFile=new RandomAccessFile(localFile_bak,"rw");//隨機方位讀取  
            raFile.seek(fileSize);//定位指針到fileSize位置  
            bis = new BufferedInputStream(urlc.getInputStream());  
            while ((len = bis.read(bt)) > 0){//循環獲取文件  
              raFile.write(bt, 0, len);  
            }  
            System.out.println("文件續傳接收完畢!");          
          }  
          else{//採用原始下載  
            fos = new FileOutputStream(localFile_bak); //沒有下載完畢就將文件的擴展名命名.bak  
            dos = new DataOutputStream(fos);  
            bis = new BufferedInputStream(urlc.getInputStream());          
            System.out.println("正在接收文件...");  
            int test=0;  
            while ((len = bis.read(bt)) > 0){//循環獲取文件  
              dos.write(bt, 0, len);  
              test++;  
            }          
          }        
          System.out.println("共用時:" +   
                             (System.currentTimeMillis() - start) / 1000);  
          if(bis!=null)  
            bis.close();  
          if(dos!=null)  
            dos.close();  
          if(fos!=null)  
            fos.close();  
          if(raFile!=null)  
            raFile.close();  
          System.out.println("localFile_bak:"+gco.FileSize(localFile_bak));  
          if(gco.FileSize(localFile_bak)==TotalSize){//下載完畢後,將文件重命名  
            gco.FileRename(localFile_bak,localFile);  
          }  
          System.exit(0);  
        }  
        catch (Exception e){  
          try{  
            if(bis!=null)  
              bis.close();  
            if(dos!=null)  
              dos.close();  
            if(fos!=null)  
              fos.close();  
            if(raFile!=null)  
              raFile.close();  
          }  
          catch (IOException f){  
            f.printStackTrace();  
          }  
          e.printStackTrace();  
        }  
        System.exit(0);  
      }
相關文章
相關標籤/搜索