慕课week8 多线程下载多个网页文件

 下面的程序可以下载多个网页文件(download方法已写好),请将它改成多线程进行下载(评分占7分),如果可能, 显示计算全部下载完成程序所用的时间(提示:new Date().getTime()可以得到当前时间的毫秒数,评分占3分)。

另外,请注意一下, ‎ ‍系统中对dstng.com的网址由于http:传到平台后,它自动改成了https:,所以请改回http:。 另外,有一些链接访问不了,所以要注意加try...catch。

import java.net.URL;
import java.io.*;
 
class Downloader 
{
    public static void main(String[] args)
        throws Exception
    {
        final URL[] urls = {
            new URL("https://www.pku.edu.cn"),
            new URL("https://www.baidu.com"),
            new URL("https://www.sina.com.cn"),
        };
        final String[] files = {
            "pku.htm", 
            "baidu.htm",
            "sina.htm", 
        };
 
        for(int idx=0; idx<urls.length; idx++){
            try{
                System.out.println( urls[idx] );
                download( urls[idx], files[idx]);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
    static void download( URL url, String file)
        throws IOException
    {
        try(InputStream input = url.openStream();
            OutputStream output = new FileOutputStream(file))
        {
            byte[] data = new byte[1024];
            int length;
            while((length=input.read(data))!=-1){
                output.write(data,0,length);
            }
        }
    }
}
最终代码: 

这里通过继承Thread类来实现多线程

import java.net.URL;
import java.io.*;
import java.util.Date;

class Downloader extends Thread{
    URL url;String file;
    public Downloader( URL url, String file){
        this.url=url;
        this.file=file;
        System.out.println( url);
    }

    public void run(){
        java.io.InputStream input = null ;
        java.io.OutputStream output = null ;
        try{

            input =  url.openStream();
            output = new FileOutputStream(file);
            byte[] data = new byte[1024];
            int length;
            try {
                while((length=input.read(data))!=-1){
                    output.write(data,0,length);
                }
            }catch (IOException e1) {
                e1.printStackTrace();
            }
        }catch(IOException e1){
            e1.printStackTrace();
        }

    }


    public static void main(String[] args)
            throws Exception
    {
        final URL[] urls = {                                    //URL数组,定义网页地址
                new URL("http://www.pku.edu.cn"),
                new URL("http://www.baidu.com"),
                new URL("http://www.sina.com.cn"),
                new URL("http://www.dstang.com")
        };
        final String[] files = {                                //定义网页htm文件名
                "pku.htm",
                "baidu.htm",
                "sina.htm",
                "study.htm",
        };
        //获取初始时间
        long startTime = new Date().getTime();


        Downloader downloader1=new Downloader(urls[0],files[0]);        //启动四个线程
        Downloader downloader2=new Downloader(urls[1],files[1]);
        Downloader downloader3=new Downloader(urls[2],files[2]);
        Downloader downloader4=new Downloader(urls[3],files[3]);

        downloader1.start();
        downloader2.start();
        downloader3.start();
        downloader4.start();

        //获取全部下载完成所用总时间
        long endTime = new Date().getTime();
        System.out.println("全部下载完成,所用时间:" + (endTime - startTime) + "毫秒");


    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值