java

位置:IT落伍者 >> java >> 浏览文章

用Java实现HTTP断点续传功能(3)


发布日期:2018年11月25日
 
用Java实现HTTP断点续传功能(3)

/*

**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();

}

}

               

上一篇:一个简单JAVA网络通讯录

下一篇:Java反射机制中常用API