版权声明: https://blog.csdn.net/lyw_321/article/details/79981323
下面的程序可以下载多个网页文件(download方法已写好),请将它改成多线程进行下载,如果可能, 显示计算全部下载完成程序所用的时间(提示:new Date().getTime()可以得到当前时间的毫秒数)。
- import java.net.URL;
- import java.io.*;
- class Downloader
- {
- public static void main(String[] args)
- throws Exception
- {
- final URL[] urls = {
- 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 = {
- "pku.htm",
- "baidu.htm",
- "sina.htm",
- "study.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);
- }
- }
- }
- }
代码:
package week8;
import java.net.URL;
import java.io.*;
public class Downloader extends Thread{
String file;
URL url;
public Downloader(URL url, String file){
this.url=url;
this.file=file;
}
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 = {
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 = {
"pku.htm",
"baidu.htm",
"sina.htm",
"study.htm",
};
Thread t1=new Thread(new Downloader(urls[0],files[0]));
t1.start();
Thread t2=new Thread(new Downloader(urls[1],files[1]));
t2.start();
Thread t3=new Thread(new Downloader(urls[2],files[2]));
t3.start();
Thread t4=new Thread(new Downloader(urls[3],files[3]));
t4.start();
}
}
运行之后,在文件对应保存目录增加了4个htm文件