文件FTP上传支持断点续传demo(二)

2014-11-24 08:56:37 · 作者: · 浏览: 1
loadStatus.Remote_File_Noexist;
}

//获得远端文件大小
long lRemoteSize = files[0].getSize();

//构建输出对象
OutputStream ut = null ;

//本地存在文件,进行断点下载 ;不存在则新下载
if(f.exists()){

//构建输出对象
out = new FileOutputStream(f,true);

//本地文件大小
long localSize = f.length();


System.out.println("本地文件大小为:"+localSize);


//判定本地文件大小是否大于远程文件大小
if(localSize >= lRemoteSize){
System.out.println("本地文件大于远程文件,下载中止");
return DownloadStatus.Local_Bigger_Remote;
}

//否则进行断点续传,并记录状态
ftpClient.setRestartOffset(localSize);

InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));

byte[] bytes = new byte[1024];
long step = lRemoteSize /100;

//存放下载进度
long process=localSize /step;

int c;
while((c = in.read(bytes))!= -1){
out.write(bytes,0,c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;
if(process % 10 == 0)
System.out.println("下载进度:"+process);
//TODO 更新文件下载进度,值存放在process变量中
}
}
//下载完成关闭输入输出流对象
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if(isDo){
result = DownloadStatus.Download_From_Break_Success;
}else {
result = DownloadStatus.Download_From_Break_Failed;
}


}else {
out = new FileOutputStream(f);
InputStream in= ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=0;
long localSize = 0L;
int c;
while((c = in.read(bytes))!= -1){
out.write(bytes, 0, c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;