您现在的位置是:首页 >

用Java实现HTTP断点续传功能(2)

火烧 2021-12-11 22:08:16 1057
用Java实现HTTP断点续传功能 2   //启动子线程  fileS litterFetch = ew FileS litterFetch[ StartPo le gth]   for i t i

用Java实现HTTP断点续传功能(2)  

  //启动子线程

  fileSplitterFetch = new FileSplitterFetch[nStartPos length];

  for(int i= ;i<nStartPos length;i++)

  {

  fileSplitterFetch[i] = new FileSplitterFetch(siteInfoBean getSSiteURL()

  siteInfoBean getSFilePath() + File separator + siteInfoBean getSFileName()

  nStartPos[i] nEndPos[i] i);

  Utility log( Thread + i + nStartPos = + nStartPos[i] + nEndPos = + nEndPos[i]);

  fileSplitterFetch[i] start();

  }

  // fileSplitterFetch[nPos length ] = new FileSplitterFetch(siteInfoBean getSSiteURL()

  siteInfoBean getSFilePath() + File separator + siteInfoBean getSFileName() nPos[nPos length ] nFileLength nPos length );

  // Utility log( Thread + (nPos length ) + nStartPos = + nPos[nPos length ] +

  nEndPos = + nFileLength);

  // fileSplitterFetch[nPos length ] start();

  //等待子线程结束

  //int count = ;

  //是否结束while循环

  boolean breakWhile = false;

  while(!bStop)

  {

  write_nPos();

  Utility sleep( );

  breakWhile = true;

  for(int i= ;i<nStartPos length;i++)

  {

  if(!fileSplitterFetch[i] bDownOver)

  {

  breakWhile = false;

  break;

  }

  }

  if(breakWhile)

  break;

  //count++;

  //if(count> )

  // siteStop();

  }

  System err println( 文件下载结束! );

  }

  catch(Exception e){e printStackTrace ();}

  }

  //获得文件长度

  public long getFileSize()

  {

  int nFileLength = ;

  try{

  URL url = new URL(siteInfoBean getSSiteURL());

  HttpURLConnection Connection = (HttpURLConnection)url openConnection ();

  ( User Agent NetFox );

  int responseCode=();

  if(responseCode>= )

  {

  processErrorCode(responseCode);

  return ; // represent access is error

  }

  String sHeader;

  for(int i= ;;i++)

  {

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

  //Utility log(in readLine());

  sHeader=(i);

  if(sHeader!=null)

  {

  if(sHeader equals( Content Length ))

  {

  nFileLength = Integer parseInt((sHeader));

  break;

  }

  }

  else

  break;

  }

  }

  catch(IOException e){e printStackTrace ();}

  catch(Exception e){e printStackTrace ();}

  Utility log(nFileLength);

  return nFileLength;

  }

  //保存下载信息(文件指针位置)

  private void write_nPos()

  {

  try{

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

  output writeInt(nStartPos length);

  for(int i= ;i<nStartPos length;i++)

  {

  // output writeLong(nPos[i]);

  output writeLong(fileSplitterFetch[i] nStartPos);

用Java实现HTTP断点续传功能(2)

  output writeLong(fileSplitterFetch[i] nEndPos);

  }

  output close();

  }

  catch(IOException e){e printStackTrace ();}

  catch(Exception e){e printStackTrace ();}

  }

  //读取保存的下载信息(文件指针位置)

  private void read_nPos()

  {

  try{

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

  int nCount = input readInt();

  nStartPos = new long[nCount];

  nEndPos = new long[nCount];

  for(int i= ;i<nStartPos length;i++)

  {

  nStartPos[i] = input readLong();

  nEndPos[i] = input readLong();

  }

  input close();

  }

  catch(IOException e){e printStackTrace ();}

  catch(Exception e){e printStackTrace ();}

  }

  private void processErrorCode(int nErrorCode)

  {

  System err println( Error Code : + nErrorCode);

  }

  //停止文件下载

  public void siteStop()

  {

  bStop = true;

  for(int i= ;i<nStartPos length;i++)

  fileSplitterFetch[i] splitterStop();

  }

lishixinzhi/Article/program/Java/hx/201311/27070  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码