zoukankan      html  css  js  c++  java
  • RandomAccessFile多线程下载

    public class DownloadServer {
    
        private int threadCount = 4;
        private static String fileUrl = "https://dldir1.qq.com/qqtv/mac/TencentVideo_V2.2.1.42253.dmg";
    //    private static String fileUrl = "http://statics.garmentnet.cn/file/file_photo/show/news/5c3c055c5793d567739439.jpg";
    
        private static ExecutorService executorService = new ThreadPoolExecutor(4, 4, 180, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10), new Download(), new ThreadPoolExecutor.DiscardPolicy());
    
        private static ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1, new Download());
    
        private int getFileInfo() {
            int count = 0;
            try {
                URL url = new URL(fileUrl);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                count = urlConnection.getContentLength();
                System.out.println(count);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return count;
        }
    
    
        private void download(int fileSize) throws IOException {
            File file = new File("d.dmg");
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            }
            int perSize = fileSize / threadCount;
            int start;
            int end;
            for (int i = 0; i < threadCount; i++) {
                start = i * perSize;
                if (i == threadCount - 1) {
                    end = fileSize - 1;
                } else {
                    end = (i + 1) * perSize - 1;
                }
    
                executorService.execute(new Download(fileUrl, file, start, end));
            }
        }
    
        public static void main(String[] args) {
            DownloadServer downloadServer = new DownloadServer();
            int size = downloadServer.getFileInfo();
            scheduledExecutorService.scheduleAtFixedRate(new DownloadCount(size), 0, 10, TimeUnit.MILLISECONDS);
            try {
                downloadServer.download(size);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public class Download implements Runnable, ThreadFactory {

    private String fileUrl;
    private File file;
    private int start;
    private int end;
    public static AtomicInteger downloadFileSize = new AtomicInteger(0);
    private static AtomicInteger threadNum = new AtomicInteger(0);

    public Download(){

    }

    public Download(String fileUrl, File file, int start, int end) {
    this.file = file;
    this.start = start;
    this.end = end;
    this.fileUrl = fileUrl;
    }

    @Override
    public void run() {
    try {
    URL url = new URL(fileUrl);
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setRequestProperty("Range", "bytes=" + start + "-" + end);

    InputStream inputStream = httpURLConnection.getInputStream();
    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
    randomAccessFile.seek(start);
    byte[] bytes = new byte[1024];
    int len = 0;
    while ((len = inputStream.read(bytes)) != -1) {
    randomAccessFile.write(bytes, 0, len);
    downloadFileSize.addAndGet(len);
    // System.out.println(Thread.currentThread().getName() + "-" + downloadFileSize.addAndGet(len));
    }
    randomAccessFile.close();

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

    @Override
    public Thread newThread(Runnable r) {
    return new Thread(r, "Run-" + threadNum.incrementAndGet());
    }
    }

    public class DownloadCount implements Runnable {

    private int fileSize;
    public DownloadCount(int fileSize) {
    this.fileSize = fileSize;
    }

    @Override
    public void run() {
    if(fileSize != Download.downloadFileSize.get()) {
    System.out.println(Download.downloadFileSize);
    }
    }
    }
     
    
    
  • 相关阅读:
    避免scrollview内部控件输入时被键盘遮挡,监听键盘弹起,配合做滚动
    红包功能的开发总结
    App启动时间分析
    第三方动画库 Lottie嵌入记录
    加入一个新的团队需要做什么
    OC 面向对象的特性
    为什么说OC是运行时语言?什么是动态类型、动态绑定、动态加载?
    adb pull 和 adb push
    《重构:改善既有代码的设计》重构的方法整理
    《重构:改善既有代码的设计》(二) 代码的坏味道
  • 原文地址:https://www.cnblogs.com/sidesky/p/10617497.html
Copyright © 2011-2022 走看看