美文网首页
MultiThreadLocDownload(本地资源下载)

MultiThreadLocDownload(本地资源下载)

作者: knock | 来源:发表于2020-07-29 04:13 被阅读0次
package com.yyd.jdk;

import com.alibaba.fastjson.JSON;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/***
 * @ClassName: MultiThreadDownload
 * @Description: TODO
 * @author: yanyd
 * @Date: 1:32 2020/7/29
 * @version : V1.0
 */
public class MultiThreadLocDownload {

    private static String path ="D:\\a.txt";
    private static final String filePath = "d:\\b.txt"; //文件存放本地的路径
    private static int threadCount = 2; // 开启的线程数
    private static int runningThread = 2; // 记录已经运行的线程数量
    private static long startTime;


    /**
     * 测试下载
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        startTime = System.currentTimeMillis();

        File file=new File(path);
        FileInputStream fileInputStream=new FileInputStream(file);
        int length = fileInputStream.available();

        // 在客户端本地创建出来一个大小跟服务器端一样大小的临时文件
        RandomAccessFile raf = new RandomAccessFile(filePath, "rwd");
        // 指定创建的这个文件的长度
        raf.setLength(length);
        raf.close();
        // 假设是3个线程去下载资源。
        // 平均每一个线程下载的文件大小.
        int blockSize = length / threadCount;
        for (int threadId = 1; threadId <= threadCount; threadId++) {
            // 第一个线程下载的开始位置
            int startIndex = (threadId - 1) * blockSize;
            int endIndex = threadId * blockSize;
            if (threadId == threadCount) {// 最后一个线程下载的长度要稍微长一点
                endIndex = length;
            }
            System.out.println("线程:" + threadId + "下载:---" + startIndex + "--->" + endIndex);
            new DownLoadThread( threadId, startIndex, endIndex).start();
        }
    }


    /**
     * 下载文件的子线程,每一个线程下载对应位置的文件
     */
    public static class DownLoadThread extends Thread {
        private int threadId;
        private int startIndex;
        private int endIndex;

        /**
         * @param threadId   线程Id
         * @param startIndex 线程下载的开始位置
         * @param endIndex   线程下载的结束位置
         */
        public DownLoadThread(int threadId, int startIndex, int endIndex) {
            super();
            this.threadId = threadId;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }

        @Override
        public void run() {
            try {
                RandomAccessFile raf = new RandomAccessFile(filePath, "rwd");
                // 随机写文件的时候从哪个位置开始写  会从下一个位置开始读写
                raf.seek(startIndex);// 定位文件

                FileInputStream fileInputStream=new FileInputStream(path);
                byte[] bytes=new byte[endIndex-startIndex];
                fileInputStream.skip(startIndex);
                fileInputStream.read(bytes);

                raf.write(bytes);
                fileInputStream.close();
                raf.close();
                System.out.println("线程:" + threadId + "下载完毕");
                System.out.println((System.currentTimeMillis() - startTime));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                runningThread--;
                if (runningThread == 0) {// 所有的线程执行完毕
                    System.out.println("文件全部下载完毕!");
                }
            }
        }

    }
}

相关文章

网友评论

      本文标题:MultiThreadLocDownload(本地资源下载)

      本文链接:https://www.haomeiwen.com/subject/kgpcrktx.html