java 多线程下载 -- 断点续传

DownloadUI.java 
 
package cn.yif.URL.Download2;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;


public class DownloadUI extends JFrame implements ActionListener {
    private static final long serialVersionUID = -5035867481307416008L;

    private JLabel labURLFile;    //源文件标签
    private JLabel labLocalDir;   //目标文件标签
    private JLabel labCount;

    private JTextField tfURLFile;
    private JTextField tfLocalFile;
    private JTextField tfCount;
    private JButton btnStart;
    private JButton btnPause;
    public  JProgressBar[] bars;

    public boolean pausing = false;
    private Downloader downloader;


    public DownloadUI(){
        init();
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                downloader.saveMetaInfoTofile();
                System.exit(-1);
            }
        });

    }

    private void init(){
        this.setTitle("主窗口");
        this.setBounds(100, 100, 800, 600);
        this.setLayout(null);

        //srcFile标签
        labURLFile = new JLabel("URL文件:");
        labURLFile.setBounds(10, 10, 80, 50);
        this.add(labURLFile);

        tfURLFile = new JTextField();
        tfURLFile.setBounds(70, 20, 500, 30);
        tfURLFile.setText("http://localhost:8080/1.avi");
        this.add(tfURLFile);

        labLocalDir = new JLabel("保存路径");
        labLocalDir.setBounds(10, 60, 80, 50);
        this.add(labLocalDir);

        tfLocalFile = new JTextField();
        tfLocalFile.setBounds(70, 75, 500, 30);
        tfLocalFile.setText("D:\\1.avi");
        this.add(tfLocalFile);


        labCount = new JLabel("线程数");
        labCount.setBounds(10, 120, 80, 50);
        this.add(labCount);


        tfCount = new JTextField();
        tfCount.setBounds(70, 130, 500, 30);
        this.add(tfCount);


        btnStart = new JButton("开始");
        btnStart.setBounds(10, 170, 100, 30);
        this.add(btnStart);
        btnStart.addActionListener(this);

        btnPause = new JButton("暂停");
        btnPause.setBounds(120, 170, 100, 30);
        this.add(btnPause);
        btnPause.addActionListener(this);


        this.setVisible(true);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        //开始按钮
        if (source == btnStart){
            String url = tfURLFile.getText();
            String local = tfLocalFile.getText();
            int count = Integer.parseInt(tfCount.getText());
//            1、构造下载器
            downloader = new Downloader(url, local, count, this);
//            2、取得infos
            List<DownloadInfo> infos = downloader.getInfos();
//            3、动态添加进度条
            this.addBars(infos);

//            4、开始下载
            downloader.startDownload();
        }else if (source == btnPause){
            pausing = !pausing;
        }
    }

    private void addBars(List<DownloadInfo> infos) {
        bars = new JProgressBar[infos.size()];
        int yOffset = 210;
        for (DownloadInfo info : infos){
            bars[info.getIndex()] = new JProgressBar();
            bars[info.getIndex()].setMaximum(info.getEnd() - info.getStart() + 1);
            bars[info.getIndex()].setValue(info.getAmount());
            bars[info.getIndex()].setBounds(10, yOffset + info.getIndex() * 50 , 550, 30);
            this.add(bars[info.getIndex()]);
        }
        this.repaint();
    }

    public  void updateBar(int index, int len) {
        bars[index].setValue(bars[index].getValue() + len);
    }
}
-------------------------------------------------------------------------------------------------------

DownloadThread.java
package cn.yif.URL.Download2;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;

public class DownloadThread extends Thread{

    private DownloadInfo info;
    private DownloadUI ui;
    private Properties prop;
    public DownloadThread(DownloadInfo info, DownloadUI ui, Properties prop){
        this.info = info;
        this.ui = ui;
        this.prop = prop;
    }

    @Override
    public void run() {
        try {
            URL url = new URL(info.getURL());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Range", "bytes=" + info.getStart() + "-" + info.getEnd());
            InputStream is = conn.getInputStream();

            RandomAccessFile raf = new RandomAccessFile(info.getLocal(), "rw");
            raf.seek(info.getStart() + info.getAmount());
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = is.read(buffer)) != -1){
                while (true){
                    try {
                        if (ui.pausing){
                            Thread.sleep(200);
                        }else{
                            break;
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                raf.write(buffer, 0 , len);
                //下载更新元信息
                updateMetaFile(info.getIndex(), len);
                ui.updateBar(info.getIndex(), len);

            }
            raf.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新元数据文件
     * @param index
     * @param len
     */
    private void  updateMetaFile(int index, int len) {
        try {
            int oldAmount = Integer.parseInt(prop.getProperty("thread."+index+".amount"));
            prop.setProperty("thread."+index+".amount", String.valueOf(oldAmount + len));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
-------------------------------------------------------------------------------------------------------

DownloadInfo.java

package cn.yif.URL.Download2;

import java.io.File;

public class DownloadInfo {
    private String URL;
    private String local;
    private int start;
    private int end;
    private int index;
    private int amount = 0;

    private File metaFile;

    public File getMetaFile() {
        return metaFile;
    }

    public void setMetaFile(File metaFile) {
        this.metaFile = metaFile;
    }

    public String getURL() {
        return URL;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public void setURL(String URL) {
        this.URL = URL;
    }

    public String getLocal() {
        return local;
    }

    public void setLocal(String local) {
        this.local = local;
    }

    public int getStart() {
        return start;
    }

    public int getEnd() {
        return end;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }
}
---------------------------------------------------------------------------------------------

Downloader.java

package cn.yif.URL.Download2;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 下载器
 */
public class Downloader {
    private String url;
    private String local;
    private List<DownloadInfo> infos;
    private int count;
    private File metaFile;
    private int fileLength;
    private DownloadUI ui;
    private Properties prop;
    public Downloader(String url, String local, int count, DownloadUI ui) {
        this.url = url;
        this.local = local;
        this.count = count;
        this.ui = ui;
        prepareDownload();
    }

    public List<DownloadInfo> getInfos() {
        return infos;
    }

    /**
     * 准备下载
     */
    private void prepareDownload(){
        try {
            File localFile = new File(local);
            //e:/a.avi
            String fileName = local.substring(local.lastIndexOf("\\") + 1, local.lastIndexOf("."));
//          0、判断新传还是续传
            metaFile = new File(localFile.getParent(), fileName + ".download");

            //续传
            if (metaFile.exists()){
                processOldDownloadInfos(metaFile);
            }else {
// 新传
//          1、获取URL文件大小
                URL u = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) u.openConnection();
                fileLength = conn.getContentLength();
//          2、创建本地文件并且设置大小
                RandomAccessFile raf= new RandomAccessFile(local, "rw");
                raf.setLength(fileLength);
                raf.close();
//          3、计算下载信息集合
                initDownloadInfos();
//          4、存储下载元数据文件
                saveDownloadMetaInfo(metaFile);
            }


        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void saveDownloadMetaInfo(File metaFile) {
        try {
            Properties prop = new Properties();
            prop.setProperty("url", url);
            prop.setProperty("thread.count", String.valueOf(count));
            for (DownloadInfo info : infos){
                prop.setProperty("thread." + info.getIndex() + ".url", url);
                prop.setProperty("thread." + info.getIndex() + ".local", local );
                prop.setProperty("thread." + info.getIndex() + ".start", String.valueOf(info.getStart()));
                prop.setProperty("thread." + info.getIndex() + ".end", String.valueOf(info.getEnd()));
                prop.setProperty("thread." + info.getIndex() + ".amount", String.valueOf(info.getAmount()));
            }
            prop.store(new FileOutputStream(metaFile), null);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得上次下载线程信息
     * @param metaFile
     */
    private void processOldDownloadInfos(File metaFile) {

        try {
            infos = new ArrayList<DownloadInfo>();
            Properties prop = new Properties();
            prop.load(new FileInputStream(metaFile));
//          获得线程个数
            this.count = Integer.parseInt(prop.getProperty("thread.count"));
            this.url = prop.getProperty("url");
            DownloadInfo info = null;
            for (int i = 0; i < count; i++) {
                info = new DownloadInfo();
                info.setLocal(local);
                info.setURL(url);
                info.setIndex(i);
                info.setMetaFile(metaFile);
                info.setStart(Integer.parseInt(prop.getProperty("thread." + i + ".start")));
                info.setEnd(Integer.parseInt(prop.getProperty("thread." + i + ".end")));
                info.setAmount(Integer.parseInt(prop.getProperty("thread." + i + ".amount")));
                infos.add(info);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initDownloadInfos() {
        infos = new ArrayList<DownloadInfo>();
        DownloadInfo info = null;
        int block = fileLength / count;
        for (int i = 0; i < count; i++) {
            info = new DownloadInfo();
            info.setURL(url);
            info.setLocal(local);
            info.setIndex(i);
            info.setMetaFile(metaFile);
            info.setStart(i * block);
            if (i != count - 1){
                info.setEnd((i + 1) * block - 1);
            }else{
                info.setEnd(fileLength);
            }
            infos.add(info);
        }

    }

    public void startDownload() {
        try {
            for (DownloadInfo info : infos){
                prop = new Properties();
                prop.load(new FileInputStream(metaFile));
                new DownloadThread(info, ui, prop).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void saveMetaInfoTofile(){
        try {
            prop.store(new FileOutputStream(metaFile), null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
--------------------------------------------------------------------------------------------------
 
Download.app
package cn.yif.URL.Download2;


public class DownloadApp {
    public static void main(String[] args) {
        new DownloadUI();
    }
}

 


-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值