📄 downloader.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package mymp3.downloader;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URL;import java.text.SimpleDateFormat;import java.util.Calendar;import mymp3.AppConfig;/** * * @author huliqing */public class Downloader extends Thread{ private int state; // 状态 public static final int STATE_STOPPED = 0; // 停止的 public static final int STATE_LOADING = 1; // 运行的 public static final int STATE_PAUSED = 2; // 暂停的 public static final int STATE_OK = 3; // 完成的 private TaskModel task; private InputStream in; private OutputStream out; private byte[] buff; private int timeUsed; private int readBytes; private int totalBytes; private int speed; private final static int BUFFER_SIZE = 1024 * 10; private File file; // 完整的文件保存路径 public Downloader(TaskModel taskModel) { this.task = taskModel; Thread tt = new Thread(new Init()); tt.start(); } private class Init implements Runnable { public void run() { init(); } } private void init() { try { if (totalBytes <= 0) { URL u = new URL(task.getUrl()); totalBytes = u.openConnection().getContentLength(); } } catch (Exception ex) { System.out.println("Exception:" + this.getClass().getName()); } } @Override public void run() { makeName(); // 由主线程检查并设置文件名 File processFile = new File(file.getAbsolutePath() + "_"); // 下载中的文件名 try { if (totalBytes <= 0) { init(); } URL u = new URL(task.getUrl()); in = u.openStream(); BufferedInputStream bis = new BufferedInputStream(in); buff = new byte[BUFFER_SIZE]; out = new FileOutputStream(processFile); BufferedOutputStream bos = new BufferedOutputStream(out); int len; while (readBytes < totalBytes) { long start = System.currentTimeMillis(); len = bis.read(buff, 0, buff.length); bos.write(buff, 0, len); long end = System.currentTimeMillis(); timeUsed += (end - start); readBytes += len; synchronized (this) { if (isPaused()) { try { this.wait(); } catch (InterruptedException ie) { System.out.println("InterruptedException"); } } } if (Thread.interrupted()) { toStop(); in.close(); out.close(); return; } } // 将文件重命名 in.close(); out.close(); if (processFile.renameTo(file)) { state = STATE_OK; // 设置状态为完成 } else { System.out.println("重命名失败!"); } } catch (IOException ioe) { toStop(); System.out.println(this.getClass().getSimpleName() + ":IOException"); } } private synchronized void makeName() { // 处理完整的文件保存路径 int n = 0; String url = task.getUrl(); String filename = AppConfig.getInstance().getSavePath() + "\\" + task.getName().trim() + url.substring(url.lastIndexOf(".")); File f1 = new File(filename); File f2 = new File(filename + "_"); while (f1.exists() || f2.exists()) { n++; filename = AppConfig.getInstance().getSavePath() + "\\" + task.getName().trim() + "[" + n + "]" + url.substring(url.lastIndexOf(".")); f1 = new File(filename); f2 = new File(filename + "_"); } try { f2.createNewFile(); } catch (Exception exception) { } file = f1; } /** 开始下载任务 */ public synchronized void toStart() { if (isStopped()) { this.start(); } else if (isPaused()) { this.notifyAll(); } this.state = STATE_LOADING; } /** 停下下载任务 */ public void stopDownload() { this.state = STATE_STOPPED; this.interrupt(); } private synchronized void toStop() { this.state = STATE_STOPPED; } public synchronized boolean isStopped() { return this.state == STATE_STOPPED ? true : false; } /** 暂停下载任务 */ public synchronized void toPause() { this.state = STATE_PAUSED; } public synchronized boolean isPaused() { return this.state == STATE_PAUSED ? true : false; } /** 判断任务是否已经完成 */ public boolean isOk() { return this.state == STATE_OK ? true : false; } // ------------------------------------------------------------ /** 获取下载速度 */ public int getTimeUsed() { return timeUsed; } /** 获取已经读取的字节数(bytes) */ public int getReadBytes() { return readBytes; } /** 获取文件大小字节数(bytes) */ public int getTotalBytes() { return totalBytes; } /** 获取运行速度,单位(k/s) */ public int getSpeed() { if (timeUsed <= 0) return 0; speed = readBytes / timeUsed; return speed; } /** 获取剩余时间,格式如:hh:mm:ss */ public String getLeaveTime() { if (speed <= 0) return "--:--:--"; long leaveTime = (totalBytes - readBytes) / speed; int temp = (int) (leaveTime / 1000 / 60 / 60); String hh; if (temp < 9) { hh = "0" + temp; } else { hh = String.valueOf(temp); } Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(leaveTime); SimpleDateFormat stf = new SimpleDateFormat("mm:ss"); return (hh + ":" + stf.format(cal.getTime())); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -