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

http斷點續傳簡單實現

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

   /*
  **SiteFileFetchjava
  */
  package NetFox;
  import javaio*;
  import *;
  
  
  public class SiteFileFetch extends Thread {
  
  
  SiteInfoBean siteInfoBean = null; //文件信息Bean
  long[] nStartPos; //開始位置
  long[] nEndPos; //結束位置
  FileSplitterFetch[] fileSplitterFetch; //子線程對象
  long nFileLength; //文件長度
  boolean bFirst = true; //是否第一次取文件
  boolean bStop = false; //停止標志
  File tmpFile; //文件下載的臨時信息
  DataOutputStream output; //輸出到文件的輸出流
  
  //負責整個文件的抓取控制內部線程(FileSplitterFetch類)
  public SiteFileFetch(SiteInfoBean bean) throws IOException
  {
  siteInfoBean = bean;
  //tmpFile = FilecreateTempFile (zhongnew File(beangetSFilePath()));
  tmpFile = new File(beangetSFilePath()+Fileseparator + beangetSFileName()+);
  if(tmpFileexists ())
  {
  bFirst = false;
  read_nPos();
  }
  else
  {
  nStartPos = new long[beangetNSplitter()];
  nEndPos = new long[beangetNSplitter()];
  }
  
  
  
  }
  
  
  public void run()
  {
  //獲得文件長度
  //分割文件
  //實例FileSplitterFetch
  //啟動FileSplitterFetch線程
  //等待子線程返回
  try{
  if(bFirst)
  {
  nFileLength = getFileSize();
  if(nFileLength == )
  {
  Systemerrprintln(File Length is not known!);
  }
  else if(nFileLength == )
  {
  Systemerrprintln(File is not access!);
  }
  else
  {
  for(int i=;i<nStartPoslength;i++)
  {
  nStartPos[i] = (long)(i*(nFileLength/nStartPoslength));
  }
  for(int i=;i<nEndPoslength;i++)
  {
  nEndPos[i] = nStartPos[i+];
  }
  nEndPos[nEndPoslength] = nFileLength;
  }
  }
  
  
  //啟動子線程
  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();
  
  
  }
  }
  //負責部分文件的抓取
  **FileSplitterFetchjava
  */
  package NetFox;
  
  
  import javaio*;
  import *;
  
  
  public class FileSplitterFetch extends Thread {
  
  
  String sURL; //File URL
  long nStartPos; //File Snippet Start Position
  long nEndPos; //File Snippet End Position
  int nThreadID; //Threads ID
  boolean bDownOver = false; //Downing is over
  boolean bStop = false; //Stop identical
  FileAccessI fileAccessI = null; //File Access interface
  
  
  public FileSplitterFetch(String sURLString sNamelong nStartlong nEndint id) throws IOException
  {
  thissURL = sURL;
  thisnStartPos = nStart;
  thisnEndPos = nEnd;
  nThreadID = id;
  fileAccessI = new FileAccessI(sNamenStartPos);//定位
  }
  
  
  public void run()
  {
  while(nStartPos < nEndPos && !bStop)
  {
  
  
  try{
  URL url = new URL(sURL);
  HttpURLConnection httpConnection = (HttpURLConnection)urlopenConnection ();
  (UserAgentNetFox);
  String sProperty = bytes=+nStartPos+;
  (RANGEsProperty);
  Utilitylog(sProperty);
  
  
  InputStream input = ();
  //logResponseHead(httpConnection);
  
  
  byte[] b = new byte[];
  int nRead;
  while((nRead=inputread(b)) > && nStartPos < nEndPos && !bStop)
  {
  nStartPos += fileAcces
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26057.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.