⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downfilesplitter.java

📁 java 多线程下载程序
💻 JAVA
字号:
package down2;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.swing.event.EventListenerList;

import down2.event.MultiDownloadEvent;
import down2.event.MultiDownloadListener;


/**
 * 
 * <p>Title: DownFileSplitter.java</p>
 * <p>Description: </p>
 * <p>Copyright:OnewaveInc Copyright (c) 2007</p>
 * <p>Company: OnewaveInc</p>
 * @author Zhengrw
 * @version 3.0
 */
public class DownFileSplitter extends Thread {
	
	
	String dlURL;

	long startPos;

	long endPos;

	int threadID;

	boolean done = false;

	boolean stop = false;

	RandomAccessFile file;
	
	private EventListenerList listenerList = new EventListenerList();

	public DownFileSplitter(String dlURL, String saveAs, long nStart,
			long nEnd, int id) throws IOException {
		this.dlURL = dlURL;
		this.startPos = nStart;
		this.endPos = nEnd;
		this.threadID = id;
		file = new RandomAccessFile(saveAs, "rw");
		file.seek(startPos);
	}
	
	public void addMultiDownloadListener(MultiDownloadListener listener){
		this.listenerList.add(MultiDownloadListener.class, listener);
	}
	
	public void removeMultiDownloadListener(MultiDownloadListener listener){
		this.listenerList.remove(MultiDownloadListener.class, listener);
	}
	
	private void fireThreadStarted(int operation){
		MultiDownloadEvent event = new MultiDownloadEvent(this, threadID, operation);
		for(MultiDownloadListener listener : listenerList.getListeners(MultiDownloadListener.class)){
			listener.threadStarted(event);
		}
	}
	
	private void fireThreadStoped(int operation){
		MultiDownloadEvent event = new MultiDownloadEvent(this, threadID, operation);
		for(MultiDownloadListener listener : listenerList.getListeners(MultiDownloadListener.class)){
			listener.threadStoped(event);
		}
	}

	public void run() {
		try {
			URL url = new URL(dlURL);
			HttpURLConnection httpConnection = (HttpURLConnection) url
					.openConnection();
			String sProperty = "bytes=" + startPos + "-";
			httpConnection.setRequestProperty("RANGE", sProperty);
			System.out.println("线程" + threadID + "下载文件...请等待");
//			textArea.append("\n 线程" + threadID + "下载文件...请等待");
			fireThreadStarted(MultiDownloadEvent.DL_START);
			InputStream input = httpConnection.getInputStream();
			byte[] buf = new byte[1024];
			int offset, bytesRead;
			offset = (int) endPos - (int) startPos;
			if (offset > 1024)
				offset = 1024;
			while ((bytesRead = input.read(buf, 0, offset)) > 0
					&& startPos < endPos) {
				System.out.println("threadID: " + threadID + " started: "
						+ startPos + "  bytesRead: " + bytesRead);
				file.write(buf, 0, bytesRead);
				startPos += bytesRead;
			}

			System.out.println("线程" + threadID + "下载完毕!!");

//			textArea.append("\n 线程" + threadID + "下载完毕!!");
			fireThreadStoped(MultiDownloadEvent.DL_STOP);
			file.close();
			input.close();
			done = true;
		} catch (Exception ex) {
			fireThreadStoped(MultiDownloadEvent.DL_STOP_EXCEPTION);
			ex.printStackTrace();
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -