當前位置:編程學習大全網 - 源碼下載 - 用Java實現HTTP斷點續傳功能(2)

用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);

 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

  • 上一篇:主力控盤指標CYW輔助抄底逃頂
  • 下一篇:在歷史上具有很大影響的三國,都有過哪些偉大的發明?
  • copyright 2024編程學習大全網