发布网友 发布时间:2023-07-05 15:49
共1个回答
热心网友 时间:2024-07-24 01:43
//启动子线程
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();
}
lishixin/Article/program/Java/hx/201311/27070