多线程下载文件

·package com.my;
/**
 * 文件下载类
 * @author luweicheng
 *
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class DownFile {
    private URL fileUrl ;// 文件下载路径
    private int threadCount ;// 文件下载的线程数
    private int startPos;// 每个线程下载文件的开始位置
    private int size;// 每个线程下载文件的长度
    private int fileLength;// 文件总程度
    private String pathName ;// 下载的文件路径(包含文件名)
    private Downthread[] tDownthreads;// 线程数组

    public DownFile(URL url, int threadCount, String pathName) throws IOException {
        fileUrl = url;
        this.threadCount = threadCount;
        this.pathName = pathName;
        init();

    }

    /**
     * 初始化
     * 
     * @throws IOException
     */
    private void init() throws IOException {
        tDownthreads = new Downthread[threadCount];
        HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("connection", "keep-alive");
        fileLength = conn.getContentLength();
        System.out.println("文件长度" + fileLength);
        size = fileLength / threadCount;
        System.out.println("每个下载量==" + size);
        conn.disconnect();// 断开链接

    }

    public URL getFileUrl() {
        return fileUrl;
    }

    public int getThreadCount() {
        return this.threadCount;
    }

    /**
     * 开始下载
     */
    public void startDown() {
        for (int i = 0; i < threadCount; i++) {
            try {
                RandomAccessFile raFile = new RandomAccessFile(pathName, "rw");
                tDownthreads[i] = new Downthread(i * size, raFile, i);
                tDownthreads[i].start();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }

    }

    /**
     * 下载线程类
     * 
     * @author luweicheng
     *
     */
    class Downthread extends Thread {
        private int startPos;// 开始的位置
        private InputStream is;
        private RandomAccessFile raFile;
        private int length;// 下载的文件长度
        private int flag;// 线程标志

        public Downthread(int startPos, RandomAccessFile raFile, int i) {
            this.startPos = startPos;
            this.raFile = raFile;
            flag = i;
        }

        @Override
        public void run() {
            try {
                HttpURLConnection connection = (HttpURLConnection) fileUrl.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("connection", "keep-alive");
                connection.setConnectTimeout(5 * 1000);
                is = connection.getInputStream();
                is.skip(startPos);
                raFile.seek(startPos);
                byte[] buf = new byte[5012];
                int hasread = 0;// 读出的字节数
                // 将位置在 startPos - startPos 位置的数据读出写入
                while (length < size && (hasread = is.read(buf)) != -1) {
                    raFile.write(buf, 0, hasread);
                    length += hasread;
                    System.out.println("*****线程" + flag + "下载了*********" + length);
                }
                System.out.println("*******线程" + flag + "下载完成*********");

            } catch (IOException e) {

            } finally {

                try {
                    is.close();
                    raFile.close();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }

        }
    }

    public static void main(String[] args)throws Exception {
        URL url = new URL("http://localhost:8000/picture/a.txt");
        String pathName = "F://" + System.currentTimeMillis() + ".txt";
        DownFile downFile = new DownFile(url, 2,pathName);
        downFile.init();
        downFile.startDown();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.得到服务器下载文件的大小,然后在本地设置一个临时文件和服务器端文件大小一致 a)获得访问网络地址 b)通过URL对象的openConnection()方法打开连接,返回一个连接对象 c)设置请求头 i.setRequestMethod ii.setConnectTimeout iii.setReadTimeout d)判断是否响应成功 e)获取文件长度(getContentLength()) f)随机访问文件的读取与写入RandomAccessFile(file, mode) g)设置临时文件与服务器文件大小一致(setLength()) h)关闭临时文件 2.计算出每个线程下载的大小(开始位置,结束位置) a)计算出每个线程下载的大小 b)for循环,计算出每个线程的开始、结束位置 c)最后一个线程处理 3.每创建好一次就要开启线程下载 a)构造方法 b)通过URL对象的openConnection()方法打开连接,返回一个连接对象 c)设置请求头 i.setRequestMethod ii.setConnectTimeout d)判断是否响应成功(206) e)获取每个线程返回的流对象 f)随机访问文件的读取与写入RandomAccessFile(file, mode) g)指定开始位置 h)循环读取 i.保存每个线程下载位置 ii.记录每次下载位置 iii.关闭临时记录位置文件 iv.随机本地文件写入 v.记录已下载大小 i)关闭临时文件 j)关闭输入流 4.为了杀死线程还能继续下载的情况下,从本地文件上读取已经下载文件的开始位置 a)创建保存记录结束位置的文件 b)读取文件 c)将流转换为字符 d)获取记录位置 e)把记录位置赋给开始位置 5.当你的n个线程都下载完毕的时候我进行删除记录下载位置的缓存文件 a)线程下载完就减去 b)当没有正在运行的线程时切文件存在时删除文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值