
package com.pln.thread;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;public class TestThread2 extends Thread{ private String url; private String name;// 构造方法给私有的变量赋值 public TestThread2(String url,String name){ this.name = name; this.url = url; } @Override public void run() { WebDownLoader webDownLoader = new WebDownLoader(); try { webDownLoader.downloader(url,name); System.out.println("下载名字为:"+name); } catch (IOException e) { e.printStackTrace(); System.out.println("run方法运行异常"); } } public static void main(String[] args) {// 调用带参的构造方法 TestThread2 t1 = new TestThread2("http://www.mdjdx.cn/mdtpweb/upload/ew/uploadfile/f20196302356495401.jpg","1.jpg"); TestThread2 t2 = new TestThread2("http://www.mdjdx.cn/mdtpweb/upload/ew/uploadfile/f20196302342173219.jpg","2.jpg"); TestThread2 t3 = new TestThread2("http://www.mdjdx.cn/mdtpweb/upload/ew/uploadfile/f20198310114727.jpg","3.jpg"); t1.start();//开启线程 t2.start(); t3.start(); }}//下载器class WebDownLoader{// 下载方法 public void downloader(String url,String name) throws IOException { FileUtils.copyURLToFile(new URL(url),new File(name)); }}