熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用Java實現HTTP斷點續傳功能(2)

2022-06-13   來源: Java核心技術 

  //啟動子線程

  fileSplitterFetch = new FileSplitterFetch[nStartPoslength];

  for(int i=;i<nStartPoslength;i++)

  {

  fileSplitterFetch[i] = new FileSplitterFetch(siteInfoBeangetSSiteURL()

  siteInfoBeangetSFilePath() + Fileseparator + siteInfoBeangetSFileName()

  nStartPos[i]nEndPos[i]i);

  Utilitylog(Thread + i + nStartPos = + nStartPos[i] + nEndPos = + nEndPos[i]);

  fileSplitterFetch[i]start();

  }

  // fileSplitterFetch[nPoslength] = new FileSplitterFetch(siteInfoBeangetSSiteURL()

  siteInfoBeangetSFilePath() + Fileseparator + siteInfoBeangetSFileName()nPos[nPoslength]nFileLengthnPoslength);

  // Utilitylog(Thread + (nPoslength) + nStartPos = + nPos[nPoslength] +

  nEndPos = + nFileLength);

  // fileSplitterFetch[nPoslength]start();

  //等待子線程結束

  //int count = ;

  //是否結束while循環

  boolean breakWhile = false;

  while(!bStop)

  {

  write_nPos();

  Utilitysleep();

  breakWhile = true;

  for(int i=;i<nStartPoslength;i++)

  {

  if(!fileSplitterFetch[i]bDownOver)

  {

  breakWhile = false;

  break;

  }

  }

  if(breakWhile)

  break;

  //count++;

  //if(count>)

  // siteStop();

  }

  Systemerrprintln(文件下載結束!);

  }

  catch(Exception e){eprintStackTrace ();}

  }

  //獲得文件長度

  public long getFileSize()

  {

  int nFileLength = ;

  try{

  URL url = new URL(siteInfoBeangetSSiteURL());

  HttpURLConnection httpConnection = (HttpURLConnection)urlopenConnection ();

  (UserAgentNetFox);

  int responseCode=();

  if(responseCode>=)

  {

  processErrorCode(responseCode);

  return ; // represent access is error

  }

  String sHeader;

  for(int i=;;i++)

  {

  //DataInputStream in = new DataInputStream( ());

  //Utilitylog(inreadLine());

  sHeader=(i);

  if(sHeader!=null)

  {

  if(sHeaderequals(ContentLength))

  {

  nFileLength = IntegerparseInt((sHeader));

  break;

  }

  }

  else

  break;

  }

  }

  catch(IOException e){eprintStackTrace ();}

  catch(Exception e){eprintStackTrace ();}

  Utilitylog(nFileLength);

  return nFileLength;

  }

  //保存下載信息(文件指針位置)

  private void write_nPos()

  {

  try{

  output = new DataOutputStream(new FileOutputStream(tmpFile));

  outputwriteInt(nStartPoslength);

  for(int i=;i<nStartPoslength;i++)

  {

  // outputwriteLong(nPos[i]);

  outputwriteLong(fileSplitterFetch[i]nStartPos);

  outputwriteLong(fileSplitterFetch[i]nEndPos);

  }

  outputclose();

  }

  catch(IOException e){eprintStackTrace ();}

  catch(Exception e){eprintStackTrace ();}

  }

  //讀取保存的下載信息(文件指針位置)

  private void read_nPos()

  {

  try{

  DataInputStream input = new DataInputStream(new FileInputStream(tmpFile));

  int nCount = inputreadInt();

  nStartPos = new long[nCount];

  nEndPos = new long[nCount];

  for(int i=;i<nStartPoslength;i++)

  {

  nStartPos[i] = inputreadLong();

  nEndPos[i] = inputreadLong();

  }

  inputclose();

  }

  catch(IOException e){eprintStackTrace ();}

  catch(Exception e){eprintStackTrace ();}

  }

  private void processErrorCode(int nErrorCode)

  {

  Systemerrprintln(Error Code : + nErrorCode);

  }

  //停止文件下載

  public void siteStop()

  {

  bStop = true;

  for(int i=;i<nStartPoslength;i++)

  fileSplitterFetch[i]splitterStop();

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/27070.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.