Globle Get 多线程下载系统

系统 3592 0

GlobalGet”是实现HTTP协议和FTP协议的多线程下载工具。目前公司承担其测试版本的开发。该工具需要具备多线程分段下载的功能,同时还应实现“断点续传”功能。后续的版本还将增加下载资料管理的功能 。

运行效果如下:
Globle Get 多线程下载系统

实现代码:
    
package org.nitpro.demo;

import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadUtil {
	
	public int getFileLength(String url) throws Exception{
		int length = 0;
		URL downladURL = new URL(url);
		HttpURLConnection con = (HttpURLConnection) downladURL.openConnection();
		
		int stateCode = con.getResponseCode();
		if (stateCode != 200) {
			length =  -1;
		}
		
		int size = con.getContentLength();
		con.disconnect();
		length = size;
		return length;
	}
	
	public boolean isFinished(boolean[] isStop){
		boolean isFinished = false;
		for(int i=0;i<isStop.length;){
			boolean flag = isStop[i];
			if(!flag){
				try{
					Thread.sleep(3000);
				}catch(Exception e){
					e.printStackTrace();
				}
				isFinished = false;
			}else{
				isFinished = true;
				i++;
			}
		}
		return true;
	}
	
}

  

    
package org.nitpro.demo;

public class DownloadInfo {
	String url;
	int threadNum;
	String filename;
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public int getThreadNum() {
		return threadNum;
	}
	public void setThreadNum(int threadNum) {
		this.threadNum = threadNum;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
}


  

    
package org.nitpro.demo;

import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;

public class MultiDownload {
	int i=0;
	int fileMark = 0;
	DownloadUtil downUtil = new DownloadUtil();
	boolean isStop[] = new boolean[10];
	
	
	public void downProcess(String url,int byteCount,int threadNum) throws Exception{
		while(i<threadNum){
			final int startPosition = byteCount*i;
			final int endPosition = byteCount*(i+1);
			
			File file = new File("temp_file_"+i+".temp");
			DownThread fileThread = new DownThread(url,file,
					startPosition,endPosition,this,i);
			new Thread(fileThread).start();
			i++;
		}
	}
	
	
	public void downMulitFile(List downList) throws Exception{
		for(int k=0;k<downList.size();k++){
			DownloadInfo downInfo = (DownloadInfo)downList.get(i);
			String url = downInfo.getUrl();
			int threadNum = downInfo.getThreadNum();
			String filename = downInfo.getFilename();
			int fileLength  = downUtil.getFileLength(url);
			
			if(fileLength!=-1){
				final int byteCount = (int)(fileLength/threadNum)+1;
				boolean stopFlag = true;			
				downProcess(url,byteCount,threadNum);			
				stopFlag = downUtil.isFinished(isStop);		
				if(stopFlag){
					File file = new File(filename);
					uniteFile(threadNum,file);
				}
			}		
		}
	}
	
	
	public void uniteFile(int threadNum,File file) throws Exception{
		for(int i=0;i<threadNum;i++){
			File tempfile = new File("temp_file_"+i+".temp");
			FileInputStream fis = new FileInputStream(tempfile);
			RandomAccessFile raf = new RandomAccessFile(file, "rw");
			byte[] buf = new byte[1024];
			int len = -1;
			raf.seek((long) fileMark);
			
			while ((len = fis.read(buf)) != -1) {
				raf.write(buf, 0, len);
				fileMark += len;
			}
			
			fis.close();
			raf.close();
			tempfile.delete();
		}
	}
}


  

    
package org.nitpro.demo;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownThread implements Runnable{
	String url = "";
	File file;
	int startPosition;
	int endPosition;
	MultiDownload down;
	int i;
	
	public DownThread(String url,File file,int startPosition,int endPosition,MultiDownload down,int i){
		this.url = url;
		this.file = file;
		this.startPosition = startPosition;
		this.endPosition = endPosition;
		this.down = down;
		this.i = i;
	}
	
	public void run(){
		try{
			URL downUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection)downUrl.openConnection();
			InputStream is = connection.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is);
			
			FileOutputStream out = new FileOutputStream(file);
			byte buf[] = new byte[1024];
			int size = -1;
			int count = 0;
			bis.skip(startPosition+1);
			while ((size = bis.read(buf, 0, 1024)) > 0) {
				if ((startPosition + size) >= endPosition)
					size = endPosition - startPosition;
				out.write(buf, 0, size);
				startPosition += size;
				count += size;
			}
			bis.close();
			out.close();
			connection.disconnect();
		}catch(Exception e){
			e.printStackTrace();
		}
		down.isStop[i] = true;
	}
}

  

    
package org.nitpro.demo;

import java.util.ArrayList;
import java.util.List;

public class Demo {
	
	public void start() throws Exception{
		MultiDownload down = new MultiDownload();
		String url = "http://www.ytblog.net/blog_musfile/823996700.mp3 ";
		int threadNum = 3;
		String filename = "save_as_filename.mp3";
		
		DownloadInfo info = new DownloadInfo();
		info.setUrl(url);
		info.setThreadNum(threadNum);
		info.setFilename(filename);
		
		List downlist = new ArrayList();
		downlist.add(info);
		down.downMulitFile(downlist);
	}
	
	
	public static void main(String[] args){
		try{
			Demo demo = new Demo();
			demo.start();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

  

Globle Get 多线程下载系统


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论