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

Globle Get 多線程下載系統

系統 3899 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條評論
主站蜘蛛池模板: 五月婷花| 四虎永久免费最新在线 | 国产农村妇女一级毛片 | 国产舐足视频在线观看 | 一区视频在线播放 | 亚洲国产精品xo在线观看 | 猫咪www免费人成在线观看网址 | 玖玖在线资源站 | 日韩欧美色综合 | 日韩一区二区三区视频在线观看 | 国产精品久久久久不卡绿巨人 | 亚洲精品国产美女在线观看 | 亚洲乱码视频在线观看 | 中文字幕一区二区三区精彩视频 | 日韩毛片免费线上观看 | 久久我们这里只有精品国产4 | 在线观看福利影院 | 国产在线精品一区二区三区不卡 | 日韩中文字幕网 | 成人97在线观看免费高清 | 亚洲精品久久激情影院 | 亚洲精品国产手机 | 国产精品二区高清在线 | 国内一区亚洲综合图区欧美 | 色综合合久久天天综合绕视看 | 色综合久久六月婷婷中文字幕 | 国产成人亚洲欧美三区综合 | 国产综合久久一区二区三区 | 亚洲综合久久综合激情久久 | 国产在线19禁免费观看 | 777色狠狠一区二区三区香蕉 | 成人免费淫片免费观看 | 香蕉人在线香蕉人在线 | 国产高清免费午夜在线视频 | 国产在线播放一区二区 | 咪咪色综合 | 午夜视频色 | 日日干日日爽 | 国产精品一区二区不卡 | 亚洲欧美日韩网站 | 这里只有精品久久 |