博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【247天】我爱刷题系列(6)
阅读量:7192 次
发布时间:2019-06-29

本文共 3559 字,大约阅读时间需要 11 分钟。

叨叨两句

  1. 越来越难啦!干死它们!

题15: 多线程下载

package com.test;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.net.URL;import java.net.URLConnection;public class DownloadThread extends Thread{        //正在运行的线程个数    public static int threadCount = 0;        private int startIndex;    private int endIndex;    private String path;    private int id;        public DownloadThread() {            }        public DownloadThread(int startIndex, int endIndex, String path, int id) {        this.startIndex = startIndex;        this.endIndex = endIndex;        this.path = path;        this.id = id;        threadCount++;    }        public void run() {        try {            //创建URL对象            URL url = new URL(path);            //建立连接            URLConnection conn = url.openConnection();            //配置连接            conn.setRequestProperty("Range","bytes" + startIndex + "-" + endIndex);            //通过连接获取流            InputStream is = conn.getInputStream();                        FileOutputStream fos = new FileOutputStream(id + ".temp");                        byte[] arr = new byte[8 * 1024];            int len;                        while((len = is.read(arr)) != -1) {                fos.write(arr, 0, len);            }                        is.close();            fos.close();                        threadCount--;            System.out.println(getName() + "下载完毕");                    } catch (Exception e) {            e.printStackTrace();        }    }}class FileUtil {    public static void mergeFile(String srcPath,String destPath) throws Exception {        File file = new File(srcPath);        File[] subFiles = file.listFiles(new FileFilter() {            public boolean accept(File pathname) {                if(pathname.isFile() && pathname.getName().endsWith(".temp")) {                    return true;                }                return false;            }        });                FileOutputStream fos = new FileOutputStream(destPath);        for (File subFile : subFiles) {            FileInputStream fis = new FileInputStream(subFile);            byte[] arr = new byte[8 * 1024];            int len;            while((len = fis.read(arr)) != -1) {                fos.write(arr, 0, len);            }            fis.close();        }        fos.close();        System.out.println("合并完毕");    }}class ServerUtils {    public static int getFileLength(String path) {        try {            URL url = new URL(path);            URLConnection conn = url.openConnection();            int fileLength = conn.getContentLength();            return fileLength;        } catch(Exception e) {            return -1;        }    }}
package com.test;public class Test05 {    public static void main(String[] args) throws Exception {        String path = "http://ox4j4dsnp.bkt.clouddn.com/17-10-1/58878601.jpg";                int length = ServerUtils.getFileLength(path);        int count = 6;        int perLength = length / count;        for(int i = 0; i < count; i++) {            int startIndex = perLength * i;            int endIndex = perLength * (i + 1) - 1;            if(i == count - 1) {                endIndex = length - 1;                System.out.println(endIndex);            }            new DownloadThread(startIndex, endIndex, path, i).start();        }                while(DownloadThread.threadCount > 0) {            Thread.sleep(20);        }                FileUtil.mergeFile("D:\\workspace\\test","D:\\workspace\\test\\copy.jpg");    }}

转载地址:http://gzxkm.baihongyu.com/

你可能感兴趣的文章
二分搜索找到所在区间
查看>>
dockerfile构建nginx服务
查看>>
JVM(java虚拟机)工作原理
查看>>
第五届金梧奖移动广告创意节暨移动营销峰会2019(上海)
查看>>
vbox和宿主机共享文件夹
查看>>
剑指offer中经典的算法题之从头到尾打印链表
查看>>
GZFramwork数据库层《前言》DLL项目引用
查看>>
响应式Web设计的9项基本原则
查看>>
Linux:用gcc编译为32位程序
查看>>
教你如何实现android上的九点连线锁
查看>>
学习数组的代码
查看>>
java学习笔记第一章
查看>>
基础控件之UIButton.UIImageView基本属性与方法概览
查看>>
TYVJ P3522 &&洛谷 P1135 奇怪的电梯 Label:bfs
查看>>
C# 会可能需要的扩展
查看>>
彻底卸载Oracle
查看>>
bzoj2434
查看>>
银联支付集成之 ---- 安卓
查看>>
递归函数打印斐波那契数列
查看>>
rabbitmq的简单介绍二
查看>>