服务器

位置:IT落伍者 >> 服务器 >> 浏览文章

http断点续传简单实现


发布日期:2020年03月13日
 
http断点续传简单实现

/*

**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               

上一篇:使用HttpContext的User属性来实现用户验证

下一篇:HttpSessionListener实现统计在线人数