Java断点下载

package com.downloader.model;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

import com.downloader.util.URLTools;

public class Task implements Serializable {
	
	private static final long serialVersionUID = -3619398241803514013L;
	
	public static final int UNSTARTED = -2;

	public static final int PAUSED = -1;
	
	public static final int DOWNLOADING = 0;
	
	public static final int FINISHED = 1;
	
	private String id = null;
	
	private URL url = null;
	
	private File path = null;
	
	private DownloadThread downloadthread = null;
	
	private UpdateThread updatethread = null;
	
	private int size = 0;
	
	private int downloaded = 0;
	
	private int state = UNSTARTED;
	
	private double speed = 0;
	
	public Task(final URL url) throws IOException {
		// TODO Auto-generated constructor stub
		id = UUID.randomUUID().toString();
		this.url = url;
		this.path = new File(CurrentSettings.getPath(), URLTools.getFileName(url));
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		size = conn.getContentLength();
		conn.disconnect();
		downloadthread = new DownloadThread();
		updatethread = new UpdateThread();
	}
	
	public Task(final String spec) throws IOException {
		// TODO Auto-generated constructor stub
		this(new URL(spec));
	}

	public void startDownload() {
		if (state == UNSTARTED) {
			state = DOWNLOADING;
			updatethread.start();
			downloadthread.start();
		}
	}
	
	public void pauseDownload() {
		if (state == DOWNLOADING) {
			state = PAUSED;
		}
	}
	
	public void resumeDownload() {
		if (state == PAUSED) {
			state = DOWNLOADING;
			downloadthread = new DownloadThread();
			updatethread = new UpdateThread();
			updatethread.start();
			downloadthread.start();
		}
		
	}
	
	public String getId() {
		return id;
	}

	public URL getUrl() {
		return url;
	}

	public File getPath() {
		return path;
	}

	public int getSize() {
		return size;
	}

	public synchronized int getDownloaded() {
		return downloaded;
	}
	
	private synchronized void addDownloaded(int size) {
		downloaded = downloaded + size;
	}
	
	public int getState() {
		return state;
	}
	
	public double getSpeed() {
		if (state == DOWNLOADING) {
			return speed;
		} else {
			return 0;
		}
	}
	
	public double getProgress() {
		return ((double) downloaded / size);
	}

	private class UpdateThread extends Thread implements Serializable {
		
		private static final long serialVersionUID = -2462261743477870556L;

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while (state == DOWNLOADING) {
				long d1 = getDownloaded();
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				long d2 = getDownloaded();
				speed = ((d2 - d1) / 0.2) / 1024;
			}
		}
	}
	
	private class DownloadThread extends Thread implements Serializable {
		
		private static final long serialVersionUID = 1106138922362623787L;
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			HttpURLConnection connection = null;
			InputStream in = null;
			RandomAccessFile raf = null;
			try {
				connection = (HttpURLConnection) url.openConnection();
				String range = "bytes=" + getDownloaded() + "-";
				connection.setRequestProperty("Range", range);
				connection.connect();
				in = connection.getInputStream();
				raf = new RandomAccessFile(path, "rw");
				raf.seek(getDownloaded());
				byte[] buffer = new byte[CurrentSettings.getBufferSize()];
				int readed = 0;
				while ((state == DOWNLOADING) && (readed = in.read(buffer)) > 0) {
					raf.write(buffer, 0, readed);
					addDownloaded(readed);
				}
				if (state == DOWNLOADING) {
					state = FINISHED;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if (raf != null) {
						raf.close();
					}
					if (in != null) {
						in.close();
					}
					if (connection != null) {
						connection.disconnect();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值