java斷點續傳功能執行個體(java擷取遠程檔案)_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

import java.io.BufferedInputStream;
 import java.io.DataOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.net . * ;

/**
* 檔案傳送用戶端:擷取遠程檔案
 */
public class GetRemoteFile_Client_GoOn { 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:////x.x " ; // 檔案儲存的地方及檔案名稱,具體情況可以改
    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 ];
     // byte[] buffer=new byte[50*1024];
    RandomAccessFile raFile = null ;
     long TotalSize = 0 ; // 要下載的檔案總大小
    try
    {
       url = new URL( " http://www.netbox.cn/download/nbsetup.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);
           // buffer=buffer+bt;
           // System.
        }
         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 ++ ;
           if (test == 50 ) // 這裡是測試,你可以刪除這裡,就可以正常下載了
            break ;
         }       
        // System.out.println("檔案正常接收完畢!");
      }     
      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 );
   }
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.