/*
**SiteFileFetch
*/
package NetFox;
import java
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; //輸出到文件的輸出流
//負責整個文件的抓取
public SiteFileFetch(SiteInfoBean bean) throws IOException
{
siteInfoBean = bean;
//tmpFile = File
tmpFile = new File(bean
if(tmpFile
{
bFirst = false;
read_nPos();
}
else
{
nStartPos = new long[bean
nEndPos = new long[bean
}
}
public void run()
{
//獲得文件長度
//分割文件
//實例FileSplitterFetch
//啟動FileSplitterFetch線程
//等待子線程返回
try{
if(bFirst)
{
nFileLength = getFileSize();
if(nFileLength ==
{
System
}
else if(nFileLength ==
{
System
}
else
{
for(int i=
{
nStartPos[i] = (long)(i*(nFileLength/nStartPos
}
for(int i=
{
nEndPos[i] = nStartPos[i+
}
nEndPos[nEndPos
}
}
//啟動子線程
fileSplitterFetch = new FileSplitterFetch[nStartPos
for(int i=
{
fileSplitterFetch[i] = new FileSplitterFetch(siteInfoBean
siteInfoBean
nStartPos[i]
Utility
fileSplitterFetch[i]
}
// fileSplitterFetch[nPos
siteInfoBean
// Utility
nEndPos =
// fileSplitterFetch[nPos
//等待子線程結束
//int count =
//是否結束while循環
boolean breakWhile = false;
while(!bStop)
{
write_nPos();
Utility
breakWhile = true;
for(int i=
{
if(!fileSplitterFetch[i]
{
breakWhile = false;
break;
}
}
if(breakWhile)
break;
//count++;
//if(count>
// siteStop();
}
System
}
catch(Exception e){e
}
//獲得文件長度
public long getFileSize()
{
int nFileLength =
try{
URL url = new URL(siteInfoBean
HttpURLConnection httpConnection = (HttpURLConnection)url
(
int responseCode=();
if(responseCode>=
{
processErrorCode(responseCode);
return
}
String sHeader;
for(int i=
{
//DataInputStream in = new DataInputStream( ());
//Utility
sHeader=(i);
if(sHeader!=null)
{
if(sHeader
{
nFileLength = Integer
break;
}
}
else
break;
}
}
catch(IOException e){e
catch(Exception e){e
Utility
return nFileLength;
}
//保存下載信息(文件指針位置)
private void write_nPos()
{
try{
output = new DataOutputStream(new FileOutputStream(tmpFile));
output
for(int i=
{
// output
output
output
}
output
}
catch(IOException e){e
catch(Exception e){e
}
//讀取保存的下載信息(文件指針位置)
private void read_nPos()
{
try{
DataInputStream input = new DataInputStream(new FileInputStream(tmpFile));
int nCount = input
nStartPos = new long[nCount];
nEndPos = new long[nCount];
for(int i=
{
nStartPos[i] = input
nEndPos[i] = input
}
input
}
catch(IOException e){e
catch(Exception e){e
}
private void processErrorCode(int nErrorCode)
{
System
}
//停止文件下載
public void siteStop()
{
bStop = true;
for(int i=
fileSplitterFetch[i]
}
}
//負責部分文件的抓取
**FileSplitterFetch
*/
package NetFox;
import java
import
public class FileSplitterFetch extends Thread {
String sURL; //File URL
long nStartPos; //File Snippet Start Position
long nEndPos; //File Snippet End Position
int nThreadID; //Thread
boolean bDownOver = false; //Downing is over
boolean bStop = false; //Stop identical
FileAccessI fileAccessI = null; //File Access interface
public FileSplitterFetch(String sURL
{
this
this
this
nThreadID = id;
fileAccessI = new FileAccessI(sName
}
public void run()
{
while(nStartPos < nEndPos && !bStop)
{
try{
URL url = new URL(sURL);
HttpURLConnection httpConnection = (HttpURLConnection)url
(
String sProperty =
(
Utility
InputStream input = ();
//logResponseHead(httpConnection);
byte[] b = new byte[
int nRead;
while((nRead=input
{
nStartPos += fileAcces
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26057.html