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

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

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

  /*

  **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 += fileAccessIwrite(bnRead);

  //if(nThreadID == )

  // Utilitylog(nStartPos = + nStartPos + nEndPos = + nEndPos);

  }

  Utilitylog(Thread + nThreadID + is over!);

  bDownOver = true;

  //nPos = fileAccessIwrite (bnRead);

  }

  catch(Exception e){eprintStackTrace ();}

  }

  }

  //打印回應的頭信息

  public void logResponseHead(HttpURLConnection con)

  {

  for(int i=;;i++)

  {

  String header=congetHeaderFieldKey(i);

  if(header!=null)

  //responseHeadersput(header(header));

  Utilitylog(header+ : +congetHeaderField(header));

  else

  break;

  }

  }

  public void splitterStop()

  {

  bStop = true;

  }

  }

  /*

  **FileAccessjava

  */

  package NetFox;

  import javaio*;

  public class FileAccessI implements Serializable{

  RandomAccessFile oSavedFile;

  long nPos;

  public FileAccessI() throws IOException

  {

  this();

  }

  public FileAccessI(String sNamelong nPos) throws IOException

  {

  oSavedFile = new RandomAccessFile(sNamerw);

  thisnPos = nPos;

  oSavedFileseek(nPos);

  }

  public synchronized int write(byte[] bint nStartint nLen)

  {

  int n = ;

  try{

  oSavedFilewrite(bnStartnLen);

  n = nLen;

  }

  catch(IOException e)

  {

  eprintStackTrace ();

  }

  return n;

  }

  }

  /*

  **SiteInfoBeanjava

  */

  package NetFox;

  public class SiteInfoBean {

  private String sSiteURL; //Sites URL

  private String sFilePath; //Saved Files Path

  private String sFileName; //Saved Files Name

  private int nSplitter; //Count of Splited Downloading File

  public SiteInfoBean()

  {

  //default value of nSplitter is

  this();

  }

  public SiteInfoBean(String sURLString sPathString sNameint nSpiltter)

  {

  sSiteURL= sURL;

  sFilePath = sPath;

  sFileName = sName;

  thisnSplitter = nSpiltter;

  }

  public String getSSiteURL()

  {

  return sSiteURL;

  }

  public void setSSiteURL(String value)

  {

  sSiteURL = value;

  }

  public String getSFilePath()

  {

  return sFilePath;

  }

  public void setSFilePath(String value)

  {

  sFilePath = value;

  }

  public String getSFileName()

  {

  return sFileName;

  }

  public void setSFileName(String value)

  {

  sFileName = value;

  }

  public int getNSplitter()

  {

  return nSplitter;

  }

  public void setNSplitter(int nCount)

  {

  nSplitter = nCount;

  }

  }

  /*

  **Utilityjava

  */

  package NetFox;

  public class Utility {

  public Utility()

  {

  }

  public static void sleep(int nSecond)

  {

  try{

  Threadsleep(nSecond);

  }

  catch(Exception e)

  {

  eprintStackTrace ();

  }

  }

  public static void log(String sMsg)

  {

  Systemerrprintln(sMsg);

  }

  public static void log(int sMsg)

  {

  Systemerrprintln(sMsg);

  }

  }

  /*

  **TestMethodjava

  */

  package NetFox;

  public class TestMethod {

  public TestMethod()

  { ///xx/weblogicb_winexe

  try{

  SiteInfoBean bean = new SiteInfoBean(L:\\tempweblogicb_winexe);

  //SiteInfoBean bean = new SiteInfoBean(L:\\tempweblogicb_winexe);

  SiteFileFetch fileFetch = new SiteFileFetch(bean);

  fileFetchstart();

  }

  catch(Exception e){eprintStackTrace ();

  }

  }

  public static void main(String[] args)

  {

  new TestMethod();

  }

  }


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