亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Globle Get 多線程下載系統

系統 3679 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條評論
主站蜘蛛池模板: 久久天天躁狠狠躁夜夜中文字幕 | 成人a网 | 999这里只有精品 | 91精品久久久久含羞草 | 在线观看91精品国产不卡免费 | 国产热re99久久6国产精品 | 国产日韩精品一区二区在线观看 | 欧美韩国日本一区 | 91精品国产91久久久久久 | 色播性播爱播放影院 | 99热这里只有精品一区二 | 中文字幕视频在线观看 | 国产成人丝袜网站在线看 | 国产91页| 麻豆久久精品免费看国产 | 日韩一及片 | 五月婷久久 | 性视频一区二区三区免费 | 中文字幕久久精品波多野结 | 在线a毛片免费视频观看 | 夜夜超b天天 | 香港aa三级久久三级老师 | 德国女人一级毛片免费 | 丝袜亚洲精品中文字幕一区 | 四虎网址| 99这里只有精品6 | 岛国大片在线观看 | 亚洲国产精品激情在线观看 | 久久国产精品麻豆映画 | 老子影院午夜伦不卡手机 | 久久天天躁夜夜躁狠狠85麻豆 | 国产精品久久久久久久久久98 | 天天舔天天射天天操 | 狠狠色噜噜狠狠狠狠97不卡 | 亚洲国产日韩欧美综合久久 | 亚洲欧美综合国产不卡 | 久久国产视频精品 | 天天透天天插 | 国产五月天在线 | 久久久久久久国产精品影院 | 视频一区二区国产 |