//启动子线程
藤县网站建设公司成都创新互联,藤县网站设计制作,有大型网站制作公司丰富经验。已为藤县成百上千家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的藤县做网站的公司定做!
fileSplitterFetch = new FileSplitterFetch[nStartPos length];
for(int i= ;ilt;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= ;ilt;nStartPos length;i++)
{
if(!fileSplitterFetch[i] bDownOver)
{
breakWhile = false;
break;
}
}
if(breakWhile)
break;
//count++;
//if(countgt; )
// 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(responseCodegt;= )
{
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= ;ilt;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= ;ilt;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= ;ilt;nStartPos length;i++)
fileSplitterFetch[i] splitterStop();
}
lishixinzhi/Article/program/Java/hx/201311/27070
这个目前还帮不了你的这个忙,可以尝试用谷歌搜索一下你的提问,也许可以帮到你
package com.down;
import java.io.File;
import com点抗 mon.Common;
import com点抗 mon.ExceptionManager;
public class MainThreadDownFile{
private String url;
private String localsavePath;
private String localsaveName;
private int threadNum;
private static String tempStoreDirectory;//临时文件路径
private long eachThreadSzie;//平均每个线程获取文件的大小
boolean isDownStop=false;
private int entid;
public MainThreadDownFile(int entid,String url,String savePath,String saveName,int threadNum) {
this.entid=entid;
this.url=url;
this.localsavePath=savePath;
this.localsaveName=saveName;
this.threadNum=threadNum;
if(!createDirectory(localsavePath)){ //创建存放附件的地址
Common.WriteLog("创建文件失败!"散滑);
return;
}
}
/**创建存放临碰拿时文件的临冲吵腊时文件夹*/
private String getTempStoreDirectory(String tempDirctoryName){
String tempDirctory = localsavePath +"\\"+ tempDirctoryName;
String tNum = "";
do{
tempDirctory = tempDirctory + tNum;
File file = new File(tempDirctory);
if(file.exists()) tNum = "0";
else return tempDirctory;
} while(true);
}
/**判断是否有存放附件文件夹,如果没有就创建*/
private boolean createDirectory(String localFileAddress){
try{
File file = new File(localFileAddress);
if(!file.exists())
file.mkdir();
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
public void getEachThreadFileSize(long size){
eachThreadSzie=size/threadNum;
}
public boolean checkExistTPFile(){
File file = new File(tempStoreDirectory);
String fileList[] = file.list();
if(fileList.length 0){
for(int i = 0; i fileList.length; i++)
if(fileList[i].indexOf(".tp") 0)
return true;
}
return false;
}
public void init(NetManager nm) throws ExceptionManager{
try{
if(nm.getCode()==200){
String tempDirctoryName="tmp";
DownInfoWriteXml diwx=new DownInfoWriteXml(localsavePath);
String localSaveAddress=diwx.downFileExist(url); //获取临时附件地址
if(localSaveAddress!=null){
this.tempStoreDirectory=localSaveAddress;
}else{
synchronized(this){
tempStoreDirectory=getTempStoreDirectory(tempDirctoryName);
createDirectory(tempStoreDirectory);
diwx.addOneDownRecord(url, tempStoreDirectory);
diwx.saveChange();
}
}
long filesize=nm.getFileSize();
String filename=Common.getSuffixName(nm.getUrlFileName(),"/");
String tempThreadfileName=tempStoreDirectory + "\\" + filename + ".part";
getEachThreadFileSize(filesize);
SingleThreadDownFile[] stdf=new SingleThreadDownFile[threadNum];
long startPos=0L,endPos=0L;
for(int i=1;i=threadNum;i++){
if(i1) startPos+=eachThreadSzie;
endPos=startPos+eachThreadSzie;
DownInfo di=new DownInfo(i,url,startPos,endPos);
stdf[i-1]=new SingleThreadDownFile(tempThreadfileName+i);
stdf[i-1].setDi(di);
stdf[i-1].start();
}
String str[]=new String[threadNum];
boolean isStop=true;
DownState ds=new DownState(filesize);
while(isStop){
int n=0;
long fsize=0L;
long totalsize=0L;
for(int i=1;i=threadNum;i++){
if(stdf[i-1].isAlive())
fsize += stdf[i-1].downPace;
else n++;
long tsize=new Long(stdf[i-1].downSize);
if(tsize==0){
totalsize += Common.FileSize(tempThreadfileName+i);
}else{
totalsize += tsize;
}
}
if(n==threadNum) isStop=false;
ds.updateDownSize(fsize,totalsize);
ds.init(isStop);
}
if(!checkExistTPFile()!isStop){
FileCombination fc=new FileCombination(url,tempStoreDirectory,localsavePath,localsaveName);
fc.init();
isDownStop=true;
}
}else{
Common.WriteLog(nm.getHttpMessage(nm.getCode()));
}
}catch(Exception e){
throw new ExceptionManager(entid,"出现异常",e);
}
}
}
024字节)。第虚斗一次B接收了512字节,那么第二次连接A就应该从513字节开始传输。
也就是说,在第二次传输时,B要提供“我要从513字节开始传送文件F”的信息,然后A使用FileInputStream构建输入流读取本地文件,使用skip(512)方法跳过文件F的前512字节再传送文件,之后B将数据追加(append)到先前接收的文件末尾即可。
进一步考虑,如果要实现多线程传送,即分块传输,也同样的道理。假如B要求差蚂磨分作两块同时传输,那么A启动两个线程,一个从513字节读到768字节(工256字节),第二个线程从769字节到1024字节即可。
如果你要从网络上下载文件,就是说A方不是你实现的,那么你要先确认A方支不支持断电续传功能(HTTP1.1),然后你查阅下HTTP1.1协议,在HTTP1.1版本里,可以通过设置请求包头某个字段的信息(使用URLConnection创建连接并使用setRequestProperty(String key, String value) 方法设置)从而精确读取文件的某一段数据的。注意,基于HTTP断点续传的关键是1.1版本,1.0版本是不支持的。
补充:
嗯,查到了,是设置range属性,即setRequestProperty("range", "bytes=513-1024").你可以使用迅雷下载某个文件,然后从物明”线程信息“中就可以看到这个http1.1断点续传的所有行为信息了。